Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2015-2016, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are |
| 5 | * met: |
| 6 | * * Redistributions of source code must retain the above copyright |
| 7 | * notice, this list of conditions and the following disclaimer. |
| 8 | * * Redistributions in binary form must reproduce the above |
| 9 | * copyright notice, this list of conditions and the following |
| 10 | * disclaimer in the documentation and/or other materials provided |
| 11 | * with the distribution. |
| 12 | * * Neither the name of The Linux Foundation nor the names of its |
| 13 | * contributors may be used to endorse or promote products derived |
| 14 | * from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 25 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 26 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <Board.h> |
| 30 | #include <Protocol/EFICardInfo.h> |
| 31 | #include <LinuxLoaderLib.h> |
| 32 | |
Channagoud Kadabi | 539d307 | 2016-02-11 21:34:48 -0800 | [diff] [blame] | 33 | STATIC struct BoardInfo platform_board_info; |
| 34 | |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 35 | EFI_STATUS BaseMem(UINTN *BaseMemory) |
| 36 | { |
| 37 | EFI_STATUS Status = EFI_NOT_FOUND; |
| 38 | EFI_RAMPARTITION_PROTOCOL *pRamPartProtocol = NULL; |
| 39 | RamPartitionEntry *RamPartitions = NULL; |
| 40 | UINT32 NumPartitions = 0; |
| 41 | UINTN SmallestBase; |
| 42 | UINT32 i = 0; |
| 43 | |
| 44 | Status = gBS->LocateProtocol(&gEfiRamPartitionProtocolGuid, NULL, (VOID**)&pRamPartProtocol); |
| 45 | if (EFI_ERROR(Status) || (&pRamPartProtocol == NULL)) |
| 46 | { |
| 47 | DEBUG((EFI_D_ERROR, "Locate EFI_RAMPARTITION_Protocol failed, Status = (0x%x)\r\n", Status)); |
| 48 | return EFI_NOT_FOUND; |
| 49 | } |
| 50 | |
| 51 | Status = pRamPartProtocol->GetRamPartitions (pRamPartProtocol, NULL, &NumPartitions); |
| 52 | if (Status == EFI_BUFFER_TOO_SMALL) |
| 53 | { |
| 54 | RamPartitions = AllocatePool (NumPartitions * sizeof (RamPartitionEntry)); |
| 55 | if (RamPartitions == NULL) |
| 56 | return EFI_OUT_OF_RESOURCES; |
| 57 | |
| 58 | Status = pRamPartProtocol->GetRamPartitions (pRamPartProtocol, RamPartitions, &NumPartitions); |
| 59 | if (EFI_ERROR (Status) || (NumPartitions < 1) ) |
| 60 | { |
| 61 | DEBUG((EFI_D_ERROR, "Failed to get RAM partitions")); |
| 62 | return EFI_NOT_FOUND; |
| 63 | } |
| 64 | } |
| 65 | SmallestBase = RamPartitions[0].Base; |
| 66 | for (i = 0; i < NumPartitions; i++) |
| 67 | { |
| 68 | if (SmallestBase > RamPartitions[i].Base) |
| 69 | SmallestBase = RamPartitions[i].Base; |
| 70 | } |
| 71 | *BaseMemory = SmallestBase; |
| 72 | DEBUG((EFI_D_ERROR, "Memory Base Address: 0x%x\n", *BaseMemory)); |
| 73 | |
| 74 | return EFI_SUCCESS; |
| 75 | } |
| 76 | |
| 77 | STATIC EFI_STATUS GetChipInfo(struct BoardInfo *platform_board_info) |
| 78 | { |
| 79 | EFI_STATUS Status; |
| 80 | EFI_CHIPINFO_PROTOCOL *pChipInfoProtocol; |
| 81 | Status = gBS->LocateProtocol (&gEfiChipInfoProtocolGuid, NULL,(VOID **) &pChipInfoProtocol); |
| 82 | if (EFI_ERROR(Status)) |
| 83 | return Status; |
| 84 | Status = pChipInfoProtocol->GetChipId(pChipInfoProtocol, &platform_board_info->RawChipId); |
| 85 | if (EFI_ERROR(Status)) |
| 86 | return Status; |
| 87 | Status = pChipInfoProtocol->GetChipVersion(pChipInfoProtocol, &platform_board_info->ChipVersion); |
| 88 | if (EFI_ERROR(Status)) |
| 89 | return Status; |
| 90 | Status = pChipInfoProtocol->GetFoundryId(pChipInfoProtocol, &platform_board_info->FoundryId); |
| 91 | if (EFI_ERROR(Status)) |
| 92 | return Status; |
Channagoud Kadabi | 539d307 | 2016-02-11 21:34:48 -0800 | [diff] [blame] | 93 | DEBUG((EFI_D_VERBOSE, "Raw Chip Id : 0x%x\n", platform_board_info->RawChipId)); |
| 94 | DEBUG((EFI_D_VERBOSE, "Chip Version : 0x%x\n", platform_board_info->ChipVersion)); |
| 95 | DEBUG((EFI_D_VERBOSE, "Foundry Id : 0x%x\n", platform_board_info->FoundryId)); |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 96 | return Status; |
| 97 | } |
| 98 | |
| 99 | STATIC EFI_STATUS GetPlatformInfo(struct BoardInfo *platform_board_info) |
| 100 | { |
| 101 | EFI_STATUS eResult; |
| 102 | EFI_PLATFORMINFO_PROTOCOL *hPlatformInfoProtocol; |
| 103 | |
| 104 | eResult = gBS->LocateProtocol(&gEfiPlatformInfoProtocolGuid, NULL, (VOID **) &hPlatformInfoProtocol); |
| 105 | if (eResult != EFI_SUCCESS) |
| 106 | { |
| 107 | AsciiPrint("Error: Failed to locate PlatformInfo protocol.\n"); |
| 108 | goto endtest; |
| 109 | } |
| 110 | |
| 111 | eResult = hPlatformInfoProtocol->GetPlatformInfo(hPlatformInfoProtocol, &platform_board_info->PlatformInfo); |
| 112 | if (eResult != EFI_SUCCESS) |
| 113 | { |
| 114 | AsciiPrint("Error: GetPlatformInfo failed.\n"); |
| 115 | goto endtest; |
| 116 | } |
| 117 | |
| 118 | if (platform_board_info->PlatformInfo.platform >= EFI_PLATFORMINFO_NUM_TYPES) |
| 119 | { |
| 120 | AsciiPrint("Error: Unknown platform type (%d).\n", platform_board_info->PlatformInfo.platform); |
| 121 | eResult = EFI_PROTOCOL_ERROR; |
| 122 | goto endtest; |
| 123 | } |
| 124 | |
Vijay Kumar Pendoti | 02cd05b | 2016-07-19 17:50:54 +0530 | [diff] [blame^] | 125 | DEBUG((EFI_D_VERBOSE, "Platform Info : 0x%x\n", platform_board_info->PlatformInfo.platform)); |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 126 | endtest: |
| 127 | return eResult; |
| 128 | } |
| 129 | |
| 130 | STATIC EFI_STATUS GetPmicInfo(UINT32 PmicDeviceIndex, EFI_PM_DEVICE_INFO_TYPE *pmic_info) |
| 131 | { |
| 132 | EFI_STATUS Status; |
| 133 | EFI_QCOM_PMIC_VERSION_PROTOCOL *pPmicVersionProtocol; |
| 134 | Status = gBS->LocateProtocol (&gQcomPmicVersionProtocolGuid, NULL, |
| 135 | (VOID **) &pPmicVersionProtocol); |
| 136 | if (EFI_ERROR(Status)) |
Channagoud Kadabi | 5bd4099 | 2016-03-04 15:05:20 -0800 | [diff] [blame] | 137 | { |
| 138 | DEBUG((EFI_D_ERROR, "Error locating pmic protocol: %r\n", Status)); |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 139 | return Status; |
Channagoud Kadabi | 5bd4099 | 2016-03-04 15:05:20 -0800 | [diff] [blame] | 140 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 141 | Status = pPmicVersionProtocol->GetPmicInfo(PmicDeviceIndex, pmic_info); |
| 142 | if (EFI_ERROR(Status)) |
Channagoud Kadabi | 5bd4099 | 2016-03-04 15:05:20 -0800 | [diff] [blame] | 143 | { |
| 144 | DEBUG((EFI_D_ERROR, "Error getting pmic info: %r\n", Status)); |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 145 | return Status; |
Channagoud Kadabi | 5bd4099 | 2016-03-04 15:05:20 -0800 | [diff] [blame] | 146 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 147 | return Status; |
| 148 | } |
| 149 | |
| 150 | UINT32 BoardPmicModel(UINT32 PmicDeviceIndex) |
| 151 | { |
Channagoud Kadabi | 5bd4099 | 2016-03-04 15:05:20 -0800 | [diff] [blame] | 152 | EFI_STATUS Status; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 153 | EFI_PM_DEVICE_INFO_TYPE pmic_info; |
Channagoud Kadabi | 5bd4099 | 2016-03-04 15:05:20 -0800 | [diff] [blame] | 154 | Status = GetPmicInfo(PmicDeviceIndex, &pmic_info); |
| 155 | if (Status != EFI_SUCCESS) |
| 156 | { |
| 157 | DEBUG((EFI_D_ERROR, "Error getting pmic model info: %r\n", Status)); |
| 158 | ASSERT(0); |
| 159 | } |
| 160 | DEBUG((EFI_D_VERBOSE, "PMIC Model 0x%x: 0x%x\n", PmicDeviceIndex, pmic_info.PmicModel)); |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 161 | return pmic_info.PmicModel; |
| 162 | } |
| 163 | |
| 164 | UINT32 BoardPmicTarget(UINT32 PmicDeviceIndex) |
| 165 | { |
| 166 | UINT32 target; |
Channagoud Kadabi | 5bd4099 | 2016-03-04 15:05:20 -0800 | [diff] [blame] | 167 | EFI_STATUS Status; |
| 168 | |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 169 | EFI_PM_DEVICE_INFO_TYPE pmic_info; |
Channagoud Kadabi | 5bd4099 | 2016-03-04 15:05:20 -0800 | [diff] [blame] | 170 | Status = GetPmicInfo(PmicDeviceIndex, &pmic_info); |
| 171 | if (Status != EFI_SUCCESS) |
| 172 | { |
| 173 | DEBUG((EFI_D_ERROR, "Error finding board pmic info: %r\n", Status)); |
| 174 | ASSERT(0); |
| 175 | } |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 176 | target = (pmic_info.PmicAllLayerRevision << 16) | pmic_info.PmicModel; |
Channagoud Kadabi | 5bd4099 | 2016-03-04 15:05:20 -0800 | [diff] [blame] | 177 | DEBUG((EFI_D_VERBOSE, "PMIC Target 0x%x: 0x%x\n", PmicDeviceIndex, target)); |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 178 | return target; |
| 179 | } |
| 180 | |
Channagoud Kadabi | 539d307 | 2016-02-11 21:34:48 -0800 | [diff] [blame] | 181 | EFI_STATUS BoardInit() |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 182 | { |
| 183 | EFI_STATUS Status; |
Channagoud Kadabi | 539d307 | 2016-02-11 21:34:48 -0800 | [diff] [blame] | 184 | Status = GetChipInfo(&platform_board_info); |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 185 | if (EFI_ERROR(Status)) |
| 186 | return Status; |
Channagoud Kadabi | 539d307 | 2016-02-11 21:34:48 -0800 | [diff] [blame] | 187 | Status = GetPlatformInfo(&platform_board_info); |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 188 | if (EFI_ERROR(Status)) |
| 189 | return Status; |
Channagoud Kadabi | 539d307 | 2016-02-11 21:34:48 -0800 | [diff] [blame] | 190 | |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 191 | return Status; |
| 192 | } |
| 193 | |
| 194 | EFI_STATUS BoardSerialNum(CHAR8 *StrSerialNum, UINT32 Len) |
| 195 | { |
| 196 | EFI_STATUS Status = EFI_INVALID_PARAMETER; |
| 197 | MEM_CARD_INFO CardInfoData; |
| 198 | EFI_MEM_CARDINFO_PROTOCOL *CardInfo; |
Sridhar Parasuram | c8f5002 | 2015-12-05 10:36:04 -0800 | [diff] [blame] | 199 | UINT32 SerialNo; |
| 200 | HandleInfo HandleInfoList[128]; |
| 201 | UINT32 Attribs = 0; |
| 202 | UINT32 MaxHandles; |
| 203 | PartiSelectFilter HandleFilter; |
| 204 | MemCardType Type = EMMC; |
| 205 | |
| 206 | Attribs |= BLK_IO_SEL_MATCH_ROOT_DEVICE; |
| 207 | |
| 208 | MaxHandles = sizeof(HandleInfoList) / sizeof(*HandleInfoList); |
| 209 | HandleFilter.PartitionType = 0; |
| 210 | HandleFilter.VolumeName = 0; |
| 211 | HandleFilter.RootDeviceType = &gEfiEmmcUserPartitionGuid; |
| 212 | |
| 213 | Status = GetBlkIOHandles(Attribs, &HandleFilter, HandleInfoList, &MaxHandles); |
| 214 | if (EFI_ERROR (Status) || MaxHandles == 0) |
| 215 | { |
| 216 | MaxHandles = sizeof(HandleInfoList) / sizeof(*HandleInfoList); |
| 217 | HandleFilter.PartitionType = 0; |
| 218 | HandleFilter.VolumeName = 0; |
| 219 | HandleFilter.RootDeviceType = &gEfiUfsLU0Guid; |
| 220 | |
| 221 | Status = GetBlkIOHandles(Attribs, &HandleFilter, HandleInfoList, &MaxHandles); |
| 222 | if (EFI_ERROR (Status)) |
| 223 | return EFI_NOT_FOUND; |
| 224 | Type = UFS; |
| 225 | } |
| 226 | |
| 227 | Status = gBS->HandleProtocol(HandleInfoList[0].Handle, |
| 228 | &gEfiMemCardInfoProtocolGuid, |
| 229 | (VOID**)&CardInfo); |
| 230 | |
| 231 | if (Status != EFI_SUCCESS) |
| 232 | { |
| 233 | DEBUG((EFI_D_ERROR,"Error locating MemCardInfoProtocol:%x\n",Status)); |
| 234 | return Status; |
| 235 | } |
| 236 | |
| 237 | if (CardInfo->GetCardInfo (CardInfo, &CardInfoData) == EFI_SUCCESS) |
| 238 | { |
| 239 | if (Type == UFS) |
| 240 | { |
| 241 | Status = gBS->CalculateCrc32(CardInfoData.product_serial_num, CardInfoData.serial_num_len, &SerialNo); |
| 242 | if (Status != EFI_SUCCESS) |
| 243 | { |
| 244 | DEBUG((EFI_D_ERROR, "Error calculating Crc of the unicode serial number: %x\n", Status)); |
| 245 | return Status; |
| 246 | } |
| 247 | AsciiSPrint(StrSerialNum, Len, "%x", SerialNo); |
| 248 | } |
| 249 | else |
| 250 | AsciiSPrint(StrSerialNum, Len, "%x", CardInfoData.product_serial_num); |
| 251 | } |
| 252 | return Status; |
| 253 | } |
Channagoud Kadabi | 539d307 | 2016-02-11 21:34:48 -0800 | [diff] [blame] | 254 | |
| 255 | /* Helper APIs for device tree selection */ |
| 256 | UINT32 BoardPlatformRawChipId() |
| 257 | { |
| 258 | return platform_board_info.RawChipId; |
| 259 | } |
| 260 | |
| 261 | EFIChipInfoVersionType BoardPlatformChipVersion() |
| 262 | { |
| 263 | return platform_board_info.ChipVersion; |
| 264 | } |
| 265 | |
| 266 | EFIChipInfoFoundryIdType BoardPlatformFoundryId() |
| 267 | { |
| 268 | return platform_board_info.FoundryId; |
| 269 | } |
| 270 | |
| 271 | EFI_PLATFORMINFO_PLATFORM_TYPE BoardPlatformType() |
| 272 | { |
| 273 | return platform_board_info.PlatformInfo.platform; |
| 274 | } |
| 275 | |
| 276 | UINT32 BoardPlatformVersion() |
| 277 | { |
| 278 | return platform_board_info.PlatformInfo.version; |
| 279 | } |
| 280 | |
| 281 | UINT32 BoardPlatformSubType() |
| 282 | { |
| 283 | return platform_board_info.PlatformInfo.subtype; |
| 284 | } |
Channagoud Kadabi | 5bd4099 | 2016-03-04 15:05:20 -0800 | [diff] [blame] | 285 | |
| 286 | UINT32 BoardTargetId() |
| 287 | { |
| 288 | UINT32 Target; |
| 289 | |
| 290 | Target = (((platform_board_info.PlatformInfo.subtype & 0xff) << 24) | |
| 291 | (((platform_board_info.PlatformInfo.version >> 16) & 0xff) << 16) | |
| 292 | ((platform_board_info.PlatformInfo.version & 0xff) << 8) | |
| 293 | (platform_board_info.PlatformInfo.platform & 0xff)); |
| 294 | |
| 295 | return Target; |
| 296 | } |