hc
2024-03-25 edb30157bad0c0001c32b854271ace01d3b9a16a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/** @file
 
  Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
  SPDX-License-Identifier: BSD-2-Clause-Patent
**/
 
#include <PiPei.h>
#include <SaPolicyCommon.h>
#include <Library/DebugLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/HdaVerbTableLib.h>
#include <Library/IoLib.h>
#include <Library/HobLib.h>
#include <Library/PcdLib.h>
#include <Library/PchCycleDecodingLib.h>
#include <Library/PciLib.h>
#include <Library/PeiSaPolicyLib.h>
#include <Library/BoardInitLib.h>
#include <PchAccess.h>
#include <Library/GpioNativeLib.h>
#include <Library/GpioLib.h>
#include <GpioPinsSklLp.h>
#include <GpioPinsSklH.h>
#include <Library/GpioExpanderLib.h>
#include <SioRegs.h>
#include <Library/PchPcrLib.h>
#include <IoExpander.h>
#include <AttemptUsbFirst.h>
#include <PeiPlatformHookLib.h>
#include <Library/PeiPolicyInitLib.h>
#include <Library/PchInfoLib.h>
#include <FirwmareConfigurations.h>
#include "CometlakeURvpInit.h"
#include <Library/ConfigBlockLib.h>
 
EFI_STATUS
BoardFunctionInit (
  IN UINT16 BoardId
  );
 
/**
  GPIO init function for PEI post memory phase.
 
  @param[in]  BoardId       An unsigned integrer represent the board id.
 
  @retval     EFI_SUCCESS   The function completed successfully.
**/
EFI_STATUS
BoardGpioInit(
  IN UINT16 BoardId
  )
{
  //
  // GPIO Table Init.
  //
  switch (BoardId) {
 
    case BoardIdCometLakeULpddr3Rvp:
      PcdSet32S (PcdBoardGpioTable, (UINTN) mGpioTableCmlULpddr3);
      PcdSet16S (PcdBoardGpioTableSize, mGpioTableCmlULpddr3Size);
      PcdSet32S (PcdBoardGpioTable2, 0);
      PcdSet16S (PcdBoardGpioTable2Size, 0);
      break;
 
    default:
      DEBUG ((DEBUG_INFO, "For Unknown Board ID..Use Default GPIO Table...\n"));
      PcdSet32S (PcdBoardGpioTable, (UINTN) mGpioTableDefault);
      PcdSet16S (PcdBoardGpioTableSize, mGpioTableDefaultSize);
      break;
  }
 
  return EFI_SUCCESS;
}
 
/**
  Touch panel GPIO init function for PEI post memory phase.
 
  @param[in]  BoardId       An unsigned integrer represent the board id.
 
  @retval     EFI_SUCCESS   The function completed successfully.
**/
EFI_STATUS
TouchPanelGpioInit (
  IN UINT16 BoardId
  )
{
  switch (BoardId) {
    default:
      PcdSet32S (PcdBoardGpioTableTouchPanel, 0);
    break;
  }
  return EFI_SUCCESS;
}
 
/**
  Misc. init function for PEI post memory phase.
 
  @param[in]  BoardId       An unsigned integrer represent the board id.
 
  @retval     EFI_SUCCESS   The function completed successfully.
**/
EFI_STATUS
BoardMiscInit (
  IN UINT16 BoardId
  )
{
  PcdSetBoolS (PcdDebugUsbUartEnable, FALSE);
 
  switch (BoardId) {
 
    case BoardIdCometLakeULpddr3Rvp:
 
      PcdSetBoolS(PcdMipiCamGpioEnable, FALSE);
      break;
 
    default:
      PcdSetBoolS(PcdMipiCamGpioEnable, FALSE);
      break;
  }
 
  return EFI_SUCCESS;
}
 
/**
  Security GPIO init function for PEI post memory phase.
 
  @param[in]  BoardId       An unsigned integrer represent the board id.
 
  @retval     EFI_SUCCESS   The function completed successfully.
**/
EFI_STATUS
BoardSecurityInit (
  IN UINT16 BoardId
  )
{
  switch (BoardId) {
 
    case BoardIdCometLakeULpddr3Rvp:
 
      // TPM interrupt connects to GPIO_CNL_H_GPP_A_7
      PcdSet32S (PcdTpm2CurrentIrqNum, 0x1F);
      break;
  }
 
  return EFI_SUCCESS;
}
 
/**
  Board configuration initialization in the post-memory boot phase.
**/
VOID
BoardConfigInit (
  VOID
  )
{
  EFI_STATUS  Status;
  UINT16      BoardId;
 
  BoardId = BoardIdCometLakeULpddr3Rvp;
 
  Status = BoardGpioInit (BoardId);
  ASSERT_EFI_ERROR (Status);
 
  Status = TouchPanelGpioInit (BoardId);
  ASSERT_EFI_ERROR (Status);
 
  Status = HdaVerbTableInit (BoardId);
  ASSERT_EFI_ERROR (Status);
 
  Status = BoardMiscInit (BoardId);
  ASSERT_EFI_ERROR (Status);
 
  Status = BoardFunctionInit (BoardId);
  ASSERT_EFI_ERROR (Status);
 
  Status = BoardSecurityInit (BoardId);
  ASSERT_EFI_ERROR (Status);
}
 
/**
  Create the HOB for hotkey status for 'Attempt USB First' feature
 
  @retval  EFI_SUCCESS  HOB Creating successful.
  @retval  Others       HOB Creating failed.
**/
EFI_STATUS
CreateAttemptUsbFirstHotkeyInfoHob (
  VOID
  )
{
  EFI_STATUS                     Status;
  ATTEMPT_USB_FIRST_HOTKEY_INFO  AttemptUsbFirstHotkeyInfo;
 
  Status = EFI_SUCCESS;
 
  ZeroMem (
    &AttemptUsbFirstHotkeyInfo,
    sizeof (AttemptUsbFirstHotkeyInfo)
    );
 
  AttemptUsbFirstHotkeyInfo.RevisonId = 0;
  AttemptUsbFirstHotkeyInfo.HotkeyTriggered = FALSE;
 
  ///
  /// Build HOB for Attempt USB First feature
  ///
  BuildGuidDataHob (
    &gAttemptUsbFirstHotkeyInfoHobGuid,
    &(AttemptUsbFirstHotkeyInfo),
    sizeof (ATTEMPT_USB_FIRST_HOTKEY_INFO)
    );
 
  return Status;
}
 
/**
  Search and identify the physical address of a
  file module inside the FW_BINARIES_FV_SIGNED FV
 
  @retval  EFI_SUCCESS  If address has been found
  @retval  Others       If address has not been found
**/
EFI_STATUS
FindModuleInFlash2 (
  IN EFI_FIRMWARE_VOLUME_HEADER *FvHeader,
  IN EFI_GUID                   *GuidPtr,
  IN OUT UINT32                 *ModulePtr,
  IN OUT UINT32                 *ModuleSize
  )
{
  EFI_FFS_FILE_HEADER        *FfsHeader;
  EFI_FV_FILE_INFO           FileInfo;
  EFI_PEI_FILE_HANDLE        FileHandle;
  EFI_COMMON_SECTION_HEADER  *SectionHeader;
  VOID                       *FileBuffer;
  EFI_STATUS                 Status;
 
  FfsHeader = NULL;
  FileHandle = NULL;
  SectionHeader = NULL;
  FileBuffer = NULL;
 
  while (TRUE) {
    //
    // Locate FV_IMAGE file type in the FW_BINARIES_FV_SIGNED firmware volume
    //
    Status = PeiServicesFfsFindNextFile (EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE, FvHeader, &FileHandle);
    if (EFI_ERROR (Status)) {
      // unable to find FV_IMAGE file in this FV
      break;
    }
 
    FfsHeader = (EFI_FFS_FILE_HEADER*)FileHandle;
    DEBUG ((DEBUG_INFO, "FfsHeader 0x%X:\n", FfsHeader));
    DEBUG ((DEBUG_INFO, " Name = 0x%g\n", &FfsHeader->Name));
    DEBUG ((DEBUG_INFO, " Type = 0x%X\n", FfsHeader->Type));
    if (IS_FFS_FILE2 (FfsHeader)) {
      DEBUG ((DEBUG_INFO, " Size = 0x%X\n", FFS_FILE2_SIZE(FfsHeader)));
    }
    else {
      DEBUG ((DEBUG_INFO, " Size = 0x%X\n", FFS_FILE_SIZE(FfsHeader)));
    }
 
    //
    // Locate FW_BINARIES_FV FV_IMAGE Section
    //
    Status = PeiServicesFfsFindSectionData (EFI_SECTION_FIRMWARE_VOLUME_IMAGE, FileHandle, &FileBuffer);
    if (EFI_ERROR (Status)) {
      // continue to search for the next FV_IMAGE file
      DEBUG ((DEBUG_INFO, "FW_BINARIES_FV section not found. Status = %r\n", Status));
      continue;
    }
 
    SectionHeader = (EFI_COMMON_SECTION_HEADER *)FileBuffer;
    DEBUG ((DEBUG_INFO, "GUIDED SectionHeader 0x%X:\n",
    (UINT32)(UINT8 *)SectionHeader));
    if (IS_SECTION2(SectionHeader)) {
      DEBUG ((DEBUG_INFO, " Guid      = 0x%g\n",
        &((EFI_GUID_DEFINED_SECTION2 *)SectionHeader)->SectionDefinitionGuid));
      DEBUG ((DEBUG_INFO, " DataOfset = 0x%X\n",
        ((EFI_GUID_DEFINED_SECTION2 *)SectionHeader)->DataOffset));
    }
    else {
      DEBUG ((DEBUG_INFO, " Guid      = 0x%g\n",
        &((EFI_GUID_DEFINED_SECTION *)SectionHeader)->SectionDefinitionGuid));
      DEBUG ((DEBUG_INFO, " DataOfset = 0x%X\n",
        ((EFI_GUID_DEFINED_SECTION *)SectionHeader)->DataOffset));
    }
    DEBUG ((DEBUG_INFO, " Type      = 0x%X\n", SectionHeader->Type));
 
    //
    // Locate Firmware File System file within Firmware Volume
    //
    Status = PeiServicesFfsFindFileByName (GuidPtr, FileBuffer, (VOID **)&FfsHeader);
    if (EFI_ERROR (Status)) {
      // continue to search for the next FV_IMAGE file
      DEBUG ((DEBUG_INFO, "Module not found. Status = %r\n", Status));
      continue;
    }
 
    *ModulePtr = (UINT32)((UINT8 *)FfsHeader + sizeof(EFI_FFS_FILE_HEADER));
 
    //
    // Get File Information
    //
    Status = PeiServicesFfsGetFileInfo (FfsHeader, &FileInfo);
    if (!EFI_ERROR (Status)) {
      *ModuleSize = (UINT32)FileInfo.BufferSize;
      DEBUG ((DEBUG_INFO, "Module {0x%g} found at = 0x%X, Size = 0x%X\n",
        &FfsHeader->Name, *ModulePtr, *ModuleSize));
      return Status;
    }
  }
 
  return EFI_NOT_FOUND;
}
 
/**
  Get the ChipsetInit Binary pointer.
 
  @retval EFI_SUCCESS               - ChipsetInit Binary found.
  @retval EFI_NOT_FOUND             - ChipsetInit Binary not found.
**/
EFI_STATUS
UpdateChipsetInitPtr (
  VOID
  )
{
  EFI_STATUS                    Status;
  PCH_STEPPING                  PchStep;
  EFI_FIRMWARE_VOLUME_HEADER    *FvHeader;
  EFI_GUID                      *ChipsetInitBinaryGuidPtr;
  SI_POLICY_PPI                 *SiPolicyPpi;
  PCH_HSIO_CONFIG               *HsioConfig;
  UINT32                        ModuleAddr;
  UINT32                        ModuleSize;
 
  ModuleAddr = 0;
  ModuleSize = 0;
  PchStep = PchStepping ();
 
  Status = PeiServicesLocatePpi (
    &gSiPolicyPpiGuid,
    0,
    NULL,
    (VOID **)&SiPolicyPpi
    );
  ASSERT_EFI_ERROR (Status);
 
  Status = GetConfigBlock ((VOID *)SiPolicyPpi, &gHsioConfigGuid, (VOID *)&HsioConfig);
  ASSERT_EFI_ERROR (Status);
 
  ChipsetInitBinaryGuidPtr = NULL;
  if (IsPchLp()) {
    switch (PchStep) {
      case PCH_A0:
      case PCH_D0:
      case PCH_D1:
        ChipsetInitBinaryGuidPtr = &gCnlPchLpChipsetInitTableDxGuid;
        DEBUG ((DEBUG_INFO, "Using CnlPchLpChipsetInitTable_Dx table \n"));
        break;
      default:
        return EFI_NOT_FOUND;
    }
  } else {
    return EFI_NOT_FOUND;
  }
 
  //
  // Locate Firmware Volume header
  //
  FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) FixedPcdGet32 (PcdFlashFvPostMemoryBase);
  Status = FindModuleInFlash2 (FvHeader, ChipsetInitBinaryGuidPtr, &ModuleAddr, &ModuleSize);
  //
  // Get ChipsetInit Binary Pointer
  //
  HsioConfig->ChipsetInitBinPtr = ModuleAddr;
 
  //
  // Get File Size
  //
  HsioConfig->ChipsetInitBinLen = ModuleSize;
 
  DEBUG ((DEBUG_INFO, "ChipsetInit Binary Location: %x\n", HsioConfig->ChipsetInitBinPtr));
  DEBUG ((DEBUG_INFO, "ChipsetInit Binary Size: %x\n", HsioConfig->ChipsetInitBinLen));
 
  return Status;
}
 
/**
  Configure GPIO and SIO
 
  @retval  EFI_SUCCESS   Operation success.
**/
EFI_STATUS
EFIAPI
CometlakeURvpBoardInitBeforeSiliconInit(
  VOID
  )
{
  EFI_STATUS      Status;
  UINT8           FwConfig;
 
  BoardConfigInit ();
  //
  // Configure GPIO and SIO
  //
  Status = BoardInit ();
  ASSERT_EFI_ERROR (Status);
 
  FwConfig = FwConfigProduction;
  PeiPolicyInit (FwConfig);
 
  //
  // Create USB Boot First hotkey information HOB
  //
  CreateAttemptUsbFirstHotkeyInfoHob ();
 
  //
  // Initializing Platform Specific Programming
  //
  Status = PlatformSpecificInit ();
  ASSERT_EFI_ERROR(Status);
 
  //
  // Update ChipsetInitPtr
  //
  Status = UpdateChipsetInitPtr ();
 
  ///
  /// Do Late PCH init
  ///
  LateSiliconInit ();
 
  return EFI_SUCCESS;
}