Chandra Sai Chidipudi | 64ced63 | 2020-01-22 12:25:28 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2015, 2017-2020, The Linux Foundation. All rights reserved. |
Sridhar Parasuram | ae63fdf | 2015-11-25 11:11:12 -0800 | [diff] [blame] | 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 | #ifndef __LOCATEDEVICETREE_H__ |
| 30 | #define __LOCATEDEVICETREE_H__ |
| 31 | |
Sridhar Parasuram | ae63fdf | 2015-11-25 11:11:12 -0800 | [diff] [blame] | 32 | #include "Board.h" |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 33 | #include "libfdt.h" |
| 34 | #include "list.h" |
Vijay Kumar Pendoti | 4229f7b | 2017-05-08 21:32:39 +0530 | [diff] [blame] | 35 | #include <Library/BootLinux.h> |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 36 | #include <Library/DebugLib.h> |
| 37 | #include <Library/MemoryAllocationLib.h> |
| 38 | #include <Library/UefiBootServicesTableLib.h> |
| 39 | #include <Library/UefiLib.h> |
lijuang | f0bbcad | 2017-08-16 16:59:18 +0800 | [diff] [blame] | 40 | #include <Library/VerifiedBoot.h> |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 41 | #include <Protocol/EFIChipInfo.h> |
| 42 | #include <Protocol/EFIPlatformInfo.h> |
| 43 | #include <Protocol/EFIPmicVersion.h> |
| 44 | #include <Uefi.h> |
Sridhar Parasuram | ae63fdf | 2015-11-25 11:11:12 -0800 | [diff] [blame] | 45 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 46 | #define DEV_TREE_SUCCESS 0 |
| 47 | #define DEV_TREE_MAGIC 0x54444351 /* "QCDT" */ |
| 48 | #define DEV_TREE_MAGIC_LEN 4 |
| 49 | #define DEV_TREE_VERSION_V1 1 |
| 50 | #define DEV_TREE_VERSION_V2 2 |
| 51 | #define DEV_TREE_VERSION_V3 3 |
Sridhar Parasuram | ae63fdf | 2015-11-25 11:11:12 -0800 | [diff] [blame] | 52 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 53 | #define DEV_TREE_HEADER_SIZE 12 |
| 54 | #define DEVICE_TREE_IMAGE_OFFSET 0x5F8800 |
Sridhar Parasuram | ae63fdf | 2015-11-25 11:11:12 -0800 | [diff] [blame] | 55 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 56 | #define DTB_MAGIC 0xedfe0dd0 |
| 57 | #define DTB_OFFSET 0X2C |
Sridhar Parasuram | ae63fdf | 2015-11-25 11:11:12 -0800 | [diff] [blame] | 58 | |
Mayank Grover | 2676fa4 | 2019-04-16 18:43:03 +0530 | [diff] [blame] | 59 | #define DTB_PAD_SIZE 2048 |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 60 | #define DTBO_TABLE_MAGIC 0xD7B7AB1E |
| 61 | #define DTBO_CUSTOM_MAX 4 |
| 62 | #define PLATFORM_FOUNDRY_SHIFT 16 |
Bhanuprakash Modem | 03906e3 | 2018-06-08 15:16:32 +0530 | [diff] [blame] | 63 | #define DTBO_MAX_SIZE_ALLOWED (24 * 1024 * 1024) |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 64 | #define SOC_MASK (0xffff) |
| 65 | #define VARIANT_MASK (0x000000ff) |
| 66 | #define VARIANT_MINOR_MASK (0x0000ff00) |
| 67 | #define VARIANT_MAJOR_MASK (0x00ff0000) |
| 68 | #define PMIC_MODEL_MASK (0x000000ff) |
| 69 | #define PMIC_REV_MASK (0xffffff00) |
| 70 | #define PMIC_SHIFT_IDX (2) |
Parth Dixit | d488765 | 2017-09-13 22:28:52 +0530 | [diff] [blame] | 71 | #define PLATFORM_SUBTYPE_SHIFT_ID (0x18) |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 72 | #define FOUNDRY_ID_MASK (0x00ff0000) |
Chandra Sai Chidipudi | 64ced63 | 2020-01-22 12:25:28 +0530 | [diff] [blame] | 73 | #define PLATFORM_SUBTYPE_MASK (0x000000ff) |
| 74 | #define DDR_MASK (0x00000700) |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 75 | |
| 76 | typedef enum { |
Parth Dixit | d488765 | 2017-09-13 22:28:52 +0530 | [diff] [blame] | 77 | NONE_MATCH, |
| 78 | PMIC_MATCH_BEST_REV_IDX0, |
| 79 | PMIC_MATCH_EXACT_REV_IDX0, |
| 80 | PMIC_MATCH_BEST_REV_IDX1, |
| 81 | PMIC_MATCH_EXACT_REV_IDX1, |
| 82 | PMIC_MATCH_BEST_REV_IDX2, |
| 83 | PMIC_MATCH_EXACT_REV_IDX2, |
| 84 | PMIC_MATCH_BEST_REV_IDX3, |
| 85 | PMIC_MATCH_EXACT_REV_IDX3, |
| 86 | VARIANT_MINOR_BEST_MATCH, |
| 87 | VARIANT_MINOR_EXACT_MATCH, |
| 88 | VARIANT_MAJOR_BEST_MATCH, |
| 89 | VARIANT_MAJOR_EXACT_MATCH, |
| 90 | VERSION_BEST_MATCH, |
| 91 | VERSION_EXACT_MATCH, |
| 92 | FOUNDRYID_DEFAULT_MATCH, |
| 93 | FOUNDRYID_EXACT_MATCH, |
| 94 | PMIC_MATCH_DEFAULT_MODEL_IDX0, |
| 95 | PMIC_MATCH_EXACT_MODEL_IDX0, |
| 96 | PMIC_MATCH_DEFAULT_MODEL_IDX1, |
| 97 | PMIC_MATCH_EXACT_MODEL_IDX1, |
| 98 | PMIC_MATCH_DEFAULT_MODEL_IDX2, |
| 99 | PMIC_MATCH_EXACT_MODEL_IDX2, |
| 100 | PMIC_MATCH_DEFAULT_MODEL_IDX3, |
| 101 | PMIC_MATCH_EXACT_MODEL_IDX3, |
| 102 | SUBTYPE_DEFAULT_MATCH, |
| 103 | SUBTYPE_EXACT_MATCH, |
Chandra Sai Chidipudi | 64ced63 | 2020-01-22 12:25:28 +0530 | [diff] [blame] | 104 | DDR_MATCH, |
Parth Dixit | d488765 | 2017-09-13 22:28:52 +0530 | [diff] [blame] | 105 | VARIANT_MATCH, |
| 106 | SOC_MATCH, |
| 107 | MAX_MATCH, |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 108 | } DTMATCH_PARAMS; |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 109 | |
| 110 | #define TOTAL_MATCH_BITS 6 |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 111 | #define ALL_BITS_SET \ |
| 112 | (BIT (SOC_MATCH) | BIT (VARIANT_MATCH) | BIT (SUBTYPE_EXACT_MATCH) | \ |
| 113 | BIT (FOUNDRYID_EXACT_MATCH) | BIT (PMIC_MATCH_EXACT_MODEL_IDX0) | \ |
| 114 | BIT (PMIC_MATCH_EXACT_MODEL_IDX1)) |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 115 | |
| 116 | typedef enum { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 117 | PMIC_IDX0, |
| 118 | PMIC_IDX1, |
| 119 | PMIC_IDX2, |
| 120 | PMIC_IDX3, |
| 121 | MAX_PMIC_IDX, |
| 122 | } PMIC_INDEXES; |
Sridhar Parasuram | ae63fdf | 2015-11-25 11:11:12 -0800 | [diff] [blame] | 123 | |
Mukesh Ojha | fa81535 | 2017-11-08 14:43:41 +0530 | [diff] [blame] | 124 | typedef struct PmicIdInfo { |
| 125 | UINT32 DtPmicModel[MAX_PMIC_IDX]; |
| 126 | UINT32 DtPmicRev[MAX_PMIC_IDX]; |
| 127 | UINT32 DtMatchVal; |
| 128 | } PmicIdInfo; |
| 129 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 130 | typedef struct DtInfo { |
Parth Dixit | d488765 | 2017-09-13 22:28:52 +0530 | [diff] [blame] | 131 | UINT32 DtPlatformId; |
| 132 | UINT32 DtSocRev; |
| 133 | UINT32 DtFoundryId; |
| 134 | UINT32 DtVariantId; |
| 135 | UINT32 DtVariantMajor; |
| 136 | UINT32 DtVariantMinor; |
| 137 | UINT32 DtPlatformSubtype; |
| 138 | UINT32 DtPmicModel[MAX_PMIC_IDX]; |
| 139 | UINT32 DtPmicRev[MAX_PMIC_IDX]; |
| 140 | UINT32 DtMatchVal; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 141 | VOID *Dtb; |
Parth Dixit | d488765 | 2017-09-13 22:28:52 +0530 | [diff] [blame] | 142 | } DtInfo; |
| 143 | |
Sridhar Parasuram | ae63fdf | 2015-11-25 11:11:12 -0800 | [diff] [blame] | 144 | /* |
| 145 | * For DTB V1: The DTB entries would be of the format |
| 146 | * qcom,msm-id = <msm8974, CDP, rev_1>; (3 * sizeof(uint32_t)) |
| 147 | * For DTB V2: The DTB entries would be of the format |
| 148 | * qcom,msm-id = <msm8974, rev_1>; (2 * sizeof(uint32_t)) |
| 149 | * qcom,board-id = <CDP, subtype_ID>; (2 * sizeof(uint32_t)) |
| 150 | * The macros below are defined based on these. |
| 151 | */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 152 | #define DT_ENTRY_V1_SIZE 0xC |
Sridhar Parasuram | ae63fdf | 2015-11-25 11:11:12 -0800 | [diff] [blame] | 153 | |
| 154 | /*Struct def for device tree entry*/ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 155 | struct dt_entry { |
| 156 | UINT32 platform_id; |
| 157 | UINT32 variant_id; |
| 158 | UINT32 board_hw_subtype; |
| 159 | UINT32 soc_rev; |
| 160 | UINT32 pmic_rev[4]; |
| 161 | UINT64 offset; |
| 162 | UINT32 size; |
Prakruthi Deepak Heragu | 9c385aa | 2019-03-20 10:54:29 -0700 | [diff] [blame] | 163 | UINT32 Idx; |
Sridhar Parasuram | ae63fdf | 2015-11-25 11:11:12 -0800 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | /*Struct def for device tree entry*/ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 167 | struct dt_entry_v1 { |
| 168 | UINT32 platform_id; |
| 169 | UINT32 variant_id; |
| 170 | UINT32 soc_rev; |
| 171 | UINT32 offset; |
| 172 | UINT32 size; |
Sridhar Parasuram | ae63fdf | 2015-11-25 11:11:12 -0800 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | /*Struct def for device tree entry*/ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 176 | struct dt_entry_v2 { |
| 177 | UINT32 platform_id; |
| 178 | UINT32 variant_id; |
| 179 | UINT32 board_hw_subtype; |
| 180 | UINT32 soc_rev; |
| 181 | UINT32 offset; |
| 182 | UINT32 size; |
Sridhar Parasuram | ae63fdf | 2015-11-25 11:11:12 -0800 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | /*Struct def for device tree table*/ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 186 | struct dt_table { |
| 187 | UINT32 magic; |
| 188 | UINT32 version; |
| 189 | UINT32 num_entries; |
Sridhar Parasuram | ae63fdf | 2015-11-25 11:11:12 -0800 | [diff] [blame] | 190 | }; |
| 191 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 192 | struct plat_id { |
| 193 | UINT32 platform_id; |
| 194 | UINT32 soc_rev; |
Sridhar Parasuram | ae63fdf | 2015-11-25 11:11:12 -0800 | [diff] [blame] | 195 | }; |
| 196 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 197 | struct board_id { |
| 198 | UINT32 variant_id; |
| 199 | UINT32 platform_subtype; |
Sridhar Parasuram | ae63fdf | 2015-11-25 11:11:12 -0800 | [diff] [blame] | 200 | }; |
| 201 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 202 | struct pmic_id { |
| 203 | UINT32 pmic_version[4]; |
Sridhar Parasuram | ae63fdf | 2015-11-25 11:11:12 -0800 | [diff] [blame] | 204 | }; |
| 205 | |
Mukesh Ojha | 8ff5b4a | 2017-12-28 17:47:19 +0530 | [diff] [blame] | 206 | #define PLAT_ID_SIZE sizeof (struct plat_id) |
| 207 | #define BOARD_ID_SIZE sizeof (struct board_id) |
| 208 | #define PMIC_ID_SIZE sizeof (struct pmic_id) |
| 209 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 210 | struct dt_mem_node_info { |
| 211 | UINT32 offset; |
| 212 | UINT32 mem_info_cnt; |
| 213 | UINT32 addr_cell_size; |
| 214 | UINT32 size_cell_size; |
Sridhar Parasuram | ae63fdf | 2015-11-25 11:11:12 -0800 | [diff] [blame] | 215 | }; |
| 216 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 217 | enum dt_entry_info { |
| 218 | DTB_FOUNDRY = 0, |
Chandra Sai Chidipudi | 3b1450c | 2020-02-07 15:06:15 +0530 | [diff] [blame] | 219 | DTB_DDR, |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 220 | DTB_SOC, |
| 221 | DTB_MAJOR_MINOR, |
| 222 | DTB_PMIC0, |
| 223 | DTB_PMIC1, |
| 224 | DTB_PMIC2, |
| 225 | DTB_PMIC3, |
| 226 | DTB_PMIC_MODEL, |
| 227 | DTB_PANEL_TYPE, |
| 228 | DTB_BOOT_DEVICE, |
Sridhar Parasuram | ae63fdf | 2015-11-25 11:11:12 -0800 | [diff] [blame] | 229 | }; |
| 230 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 231 | enum dt_err_codes { |
| 232 | DT_OP_SUCCESS, |
| 233 | DT_OP_FAILURE = -1, |
Sridhar Parasuram | ae63fdf | 2015-11-25 11:11:12 -0800 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | typedef struct dt_entry_node { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 237 | struct list_node node; |
| 238 | struct dt_entry *dt_entry_m; |
| 239 | } dt_node; |
Sridhar Parasuram | ae63fdf | 2015-11-25 11:11:12 -0800 | [diff] [blame] | 240 | |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 241 | struct DtboTableHdr { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 242 | UINT32 Magic; // DTB TABLE MAGIC |
| 243 | UINT32 TotalSize; // includes DtTableHdr + all DtTableEntry and all dtb/dtbo |
| 244 | UINT32 HeaderSize; // sizeof(DtTableHdr) |
| 245 | UINT32 DtEntrySize; // sizeof(DtTableEntry) |
| 246 | UINT32 DtEntryCount; // number of DtTableEntry |
| 247 | UINT32 DtEntryOffset; // offset to the first DtTableEntry |
| 248 | UINT32 PageSize; // flash pagesize we assume |
| 249 | UINT32 Reserved[1]; // must zeros |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | struct DtboTableEntry { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 253 | UINT32 DtSize; |
| 254 | UINT32 DtOffset; // offset from head of DtTableHdr |
| 255 | UINT32 Id; // optional, must zero if unused |
| 256 | UINT32 Rev; // optional, must zero if unused |
| 257 | UINT32 Custom[DTBO_CUSTOM_MAX]; // optional, must zero if unused |
Vijay Kumar Pendoti | 644a20b | 2017-03-15 18:22:17 +0530 | [diff] [blame] | 258 | }; |
Sridhar Parasuram | ae63fdf | 2015-11-25 11:11:12 -0800 | [diff] [blame] | 259 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 260 | VOID * |
| 261 | DeviceTreeAppended (void *kernel, |
| 262 | UINT32 kernel_size, |
| 263 | UINT32 dtb_offset, |
| 264 | void *tags); |
| 265 | VOID * |
| 266 | GetSocDtb (void *kernel, UINT32 kernel_size, UINT32 dtb_offset, void *tags); |
| 267 | BOOLEAN GetDtboNeeded (VOID); |
| 268 | VOID * |
| 269 | GetBoardDtb (BootInfo *Info, VOID *DtboImgBuffer); |
Mayank Grover | 98dcd67 | 2019-02-05 14:46:25 +0530 | [diff] [blame] | 270 | EFI_STATUS |
| 271 | GetOvrdDtb (VOID **DtboImgBuffer); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 272 | VOID |
| 273 | PopulateBoardParams (); |
Sridhar Parasuram | ae63fdf | 2015-11-25 11:11:12 -0800 | [diff] [blame] | 274 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 275 | int |
| 276 | DeviceTreeValidate (UINT8 *DeviceTreeBuff, |
| 277 | UINT32 PageSize, |
| 278 | UINT32 *DeviceTreeSize); |
lijuang | 6fa55c0 | 2018-06-22 19:19:26 +0800 | [diff] [blame] | 279 | INT32 GetDtboIdx (VOID); |
Prakruthi Deepak Heragu | 9c385aa | 2019-03-20 10:54:29 -0700 | [diff] [blame] | 280 | INT32 GetDtbIdx (VOID); |
Bhanuprakash Modem | ed797c7 | 2018-09-05 20:39:07 +0530 | [diff] [blame] | 281 | VOID DeleteDtList (struct fdt_entry_node** DtList); |
| 282 | BOOLEAN AppendToDtList (struct fdt_entry_node **DtList, |
| 283 | UINT64 Address, |
| 284 | UINT64 Size); |
Sridhar Parasuram | ae63fdf | 2015-11-25 11:11:12 -0800 | [diff] [blame] | 285 | #endif |