blob: 40f70b0848eb2043748e672f76c11eaf03d42d21 [file] [log] [blame]
Chandra Sai Chidipudi64ced632020-01-22 12:25:28 +05301/* Copyright (c) 2015, 2017-2020, The Linux Foundation. All rights reserved.
Sridhar Parasuramae63fdf2015-11-25 11:11:12 -08002 *
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 Parasuramae63fdf2015-11-25 11:11:12 -080032#include "Board.h"
Jeevan Shriram17f173d2017-10-24 22:11:07 -070033#include "libfdt.h"
34#include "list.h"
Vijay Kumar Pendoti4229f7b2017-05-08 21:32:39 +053035#include <Library/BootLinux.h>
Jeevan Shriram17f173d2017-10-24 22:11:07 -070036#include <Library/DebugLib.h>
37#include <Library/MemoryAllocationLib.h>
38#include <Library/UefiBootServicesTableLib.h>
39#include <Library/UefiLib.h>
lijuangf0bbcad2017-08-16 16:59:18 +080040#include <Library/VerifiedBoot.h>
Jeevan Shriram17f173d2017-10-24 22:11:07 -070041#include <Protocol/EFIChipInfo.h>
42#include <Protocol/EFIPlatformInfo.h>
43#include <Protocol/EFIPmicVersion.h>
44#include <Uefi.h>
Sridhar Parasuramae63fdf2015-11-25 11:11:12 -080045
Jeevan Shriram17f173d2017-10-24 22:11:07 -070046#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 Parasuramae63fdf2015-11-25 11:11:12 -080052
Jeevan Shriram17f173d2017-10-24 22:11:07 -070053#define DEV_TREE_HEADER_SIZE 12
54#define DEVICE_TREE_IMAGE_OFFSET 0x5F8800
Sridhar Parasuramae63fdf2015-11-25 11:11:12 -080055
Jeevan Shriram17f173d2017-10-24 22:11:07 -070056#define DTB_MAGIC 0xedfe0dd0
57#define DTB_OFFSET 0X2C
Sridhar Parasuramae63fdf2015-11-25 11:11:12 -080058
Mayank Grover2676fa42019-04-16 18:43:03 +053059#define DTB_PAD_SIZE 2048
Jeevan Shriram17f173d2017-10-24 22:11:07 -070060#define DTBO_TABLE_MAGIC 0xD7B7AB1E
61#define DTBO_CUSTOM_MAX 4
62#define PLATFORM_FOUNDRY_SHIFT 16
Bhanuprakash Modem03906e32018-06-08 15:16:32 +053063#define DTBO_MAX_SIZE_ALLOWED (24 * 1024 * 1024)
Jeevan Shriram17f173d2017-10-24 22:11:07 -070064#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 Dixitd4887652017-09-13 22:28:52 +053071#define PLATFORM_SUBTYPE_SHIFT_ID (0x18)
Jeevan Shriram17f173d2017-10-24 22:11:07 -070072#define FOUNDRY_ID_MASK (0x00ff0000)
Chandra Sai Chidipudi64ced632020-01-22 12:25:28 +053073#define PLATFORM_SUBTYPE_MASK (0x000000ff)
74#define DDR_MASK (0x00000700)
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +053075
76typedef enum {
Parth Dixitd4887652017-09-13 22:28:52 +053077 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 Chidipudi64ced632020-01-22 12:25:28 +0530104 DDR_MATCH,
Parth Dixitd4887652017-09-13 22:28:52 +0530105 VARIANT_MATCH,
106 SOC_MATCH,
107 MAX_MATCH,
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700108} DTMATCH_PARAMS;
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530109
110#define TOTAL_MATCH_BITS 6
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700111#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 Pendoti644a20b2017-03-15 18:22:17 +0530115
116typedef enum {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700117 PMIC_IDX0,
118 PMIC_IDX1,
119 PMIC_IDX2,
120 PMIC_IDX3,
121 MAX_PMIC_IDX,
122} PMIC_INDEXES;
Sridhar Parasuramae63fdf2015-11-25 11:11:12 -0800123
Mukesh Ojhafa815352017-11-08 14:43:41 +0530124typedef struct PmicIdInfo {
125 UINT32 DtPmicModel[MAX_PMIC_IDX];
126 UINT32 DtPmicRev[MAX_PMIC_IDX];
127 UINT32 DtMatchVal;
128} PmicIdInfo;
129
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700130typedef struct DtInfo {
Parth Dixitd4887652017-09-13 22:28:52 +0530131 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 Shriram17f173d2017-10-24 22:11:07 -0700141 VOID *Dtb;
Parth Dixitd4887652017-09-13 22:28:52 +0530142} DtInfo;
143
Sridhar Parasuramae63fdf2015-11-25 11:11:12 -0800144/*
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 Shriram17f173d2017-10-24 22:11:07 -0700152#define DT_ENTRY_V1_SIZE 0xC
Sridhar Parasuramae63fdf2015-11-25 11:11:12 -0800153
154/*Struct def for device tree entry*/
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700155struct 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 Heragu9c385aa2019-03-20 10:54:29 -0700163 UINT32 Idx;
Sridhar Parasuramae63fdf2015-11-25 11:11:12 -0800164};
165
166/*Struct def for device tree entry*/
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700167struct dt_entry_v1 {
168 UINT32 platform_id;
169 UINT32 variant_id;
170 UINT32 soc_rev;
171 UINT32 offset;
172 UINT32 size;
Sridhar Parasuramae63fdf2015-11-25 11:11:12 -0800173};
174
175/*Struct def for device tree entry*/
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700176struct 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 Parasuramae63fdf2015-11-25 11:11:12 -0800183};
184
185/*Struct def for device tree table*/
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700186struct dt_table {
187 UINT32 magic;
188 UINT32 version;
189 UINT32 num_entries;
Sridhar Parasuramae63fdf2015-11-25 11:11:12 -0800190};
191
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700192struct plat_id {
193 UINT32 platform_id;
194 UINT32 soc_rev;
Sridhar Parasuramae63fdf2015-11-25 11:11:12 -0800195};
196
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700197struct board_id {
198 UINT32 variant_id;
199 UINT32 platform_subtype;
Sridhar Parasuramae63fdf2015-11-25 11:11:12 -0800200};
201
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700202struct pmic_id {
203 UINT32 pmic_version[4];
Sridhar Parasuramae63fdf2015-11-25 11:11:12 -0800204};
205
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530206#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 Shriram17f173d2017-10-24 22:11:07 -0700210struct dt_mem_node_info {
211 UINT32 offset;
212 UINT32 mem_info_cnt;
213 UINT32 addr_cell_size;
214 UINT32 size_cell_size;
Sridhar Parasuramae63fdf2015-11-25 11:11:12 -0800215};
216
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700217enum dt_entry_info {
218 DTB_FOUNDRY = 0,
Chandra Sai Chidipudi3b1450c2020-02-07 15:06:15 +0530219 DTB_DDR,
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700220 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 Parasuramae63fdf2015-11-25 11:11:12 -0800229};
230
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700231enum dt_err_codes {
232 DT_OP_SUCCESS,
233 DT_OP_FAILURE = -1,
Sridhar Parasuramae63fdf2015-11-25 11:11:12 -0800234};
235
236typedef struct dt_entry_node {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700237 struct list_node node;
238 struct dt_entry *dt_entry_m;
239} dt_node;
Sridhar Parasuramae63fdf2015-11-25 11:11:12 -0800240
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530241struct DtboTableHdr {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700242 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 Pendoti644a20b2017-03-15 18:22:17 +0530250};
251
252struct DtboTableEntry {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700253 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 Pendoti644a20b2017-03-15 18:22:17 +0530258};
Sridhar Parasuramae63fdf2015-11-25 11:11:12 -0800259
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700260VOID *
261DeviceTreeAppended (void *kernel,
262 UINT32 kernel_size,
263 UINT32 dtb_offset,
264 void *tags);
265VOID *
266GetSocDtb (void *kernel, UINT32 kernel_size, UINT32 dtb_offset, void *tags);
267BOOLEAN GetDtboNeeded (VOID);
268VOID *
269GetBoardDtb (BootInfo *Info, VOID *DtboImgBuffer);
Mayank Grover98dcd672019-02-05 14:46:25 +0530270EFI_STATUS
271GetOvrdDtb (VOID **DtboImgBuffer);
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700272VOID
273PopulateBoardParams ();
Sridhar Parasuramae63fdf2015-11-25 11:11:12 -0800274
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700275int
276DeviceTreeValidate (UINT8 *DeviceTreeBuff,
277 UINT32 PageSize,
278 UINT32 *DeviceTreeSize);
lijuang6fa55c02018-06-22 19:19:26 +0800279INT32 GetDtboIdx (VOID);
Prakruthi Deepak Heragu9c385aa2019-03-20 10:54:29 -0700280INT32 GetDtbIdx (VOID);
Bhanuprakash Modemed797c72018-09-05 20:39:07 +0530281VOID DeleteDtList (struct fdt_entry_node** DtList);
282BOOLEAN AppendToDtList (struct fdt_entry_node **DtList,
283 UINT64 Address,
284 UINT64 Size);
Sridhar Parasuramae63fdf2015-11-25 11:11:12 -0800285#endif