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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/** @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/IoLib.h>
#include <Library/HobLib.h>
#include <Library/PcdLib.h>
#include <Library/PchCycleDecodingLib.h>
#include <Library/PciLib.h>
#include <Library/PcdLib.h>
#include <Library/BaseMemoryLib.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 "CometlakeURvpInit.h"
#include <ConfigBlock.h>
#include <ConfigBlock/MemoryConfig.h>
#include <Library/PeiServicesLib.h>
#include <Library/PchPcrLib.h>
#include <Library/PchInfoLib.h>
#include <Register/PchRegsPcr.h>
#include <Library/PchResetLib.h>
#include <Register/PchRegsLpc.h>
#include <Library/StallPpiLib.h>
#include <Library/PeiPolicyInitLib.h>
#include <Ppi/Reset.h>
#include <PlatformBoardConfig.h>
#include <GpioPinsCnlLp.h>
#include <Library/PmcLib.h>
#include <Library/PciSegmentLib.h>
#include <PeiPlatformHookLib.h>
#include <FirwmareConfigurations.h>
#include <Library/OcWdtLib.h>
 
///
/// Reset Generator I/O Port
///
#define RESET_GENERATOR_PORT           0xCF9
 
typedef struct {
  EFI_PHYSICAL_ADDRESS    BaseAddress;
  UINT64                  Length;
} MEMORY_MAP;
 
//
// Reference RCOMP resistors on motherboard - for WHL RVP1
//
GLOBAL_REMOVE_IF_UNREFERENCED const UINT16 RcompResistorSklRvp1[SA_MRC_MAX_RCOMP] = { 200, 81, 162 };
 
//
// RCOMP target values for RdOdt, WrDS, WrDSCmd, WrDSCtl, WrDSClk - for WHL RVP1
//
GLOBAL_REMOVE_IF_UNREFERENCED const UINT16 RcompTargetSklRvp1[SA_MRC_MAX_RCOMP_TARGETS] = { 100, 40, 40, 23, 40 };
 
GLOBAL_REMOVE_IF_UNREFERENCED MEMORY_MAP MmioMap[] = {
  { FixedPcdGet64 (PcdApicLocalAddress),  FixedPcdGet32 (PcdApicLocalMmioSize) },
  { FixedPcdGet64 (PcdMchBaseAddress),    FixedPcdGet32 (PcdMchMmioSize) },
  { FixedPcdGet64 (PcdDmiBaseAddress),    FixedPcdGet32 (PcdDmiMmioSize) },
  { FixedPcdGet64 (PcdEpBaseAddress),     FixedPcdGet32 (PcdEpMmioSize) },
  { FixedPcdGet64 (PcdGdxcBaseAddress),   FixedPcdGet32 (PcdGdxcMmioSize) }
};
 
EFI_STATUS
MrcConfigInit (
  IN UINT16 BoardId
  );
 
EFI_STATUS
SaGpioConfigInit (
  IN UINT16 BoardId
  );
 
EFI_STATUS
SaMiscConfigInit (
  IN UINT16         BoardId
  );
 
EFI_STATUS
RootPortClkInfoInit (
  IN UINT16 BoardId
  );
 
EFI_STATUS
UsbConfigInit (
  IN UINT16 BoardId
  );
 
EFI_STATUS
GpioGroupTierInit (
  IN UINT16 BoardId
  );
 
EFI_STATUS
GpioTablePreMemInit (
  IN UINT16 BoardId
  );
 
EFI_STATUS
PchPmConfigInit (
  IN UINT16 BoardId
  );
 
EFI_STATUS
SaDisplayConfigInit (
  IN UINT16 BoardId
  );
 
EFI_STATUS
BoardFunctionInitPreMem (
  IN UINT16 BoardId
  );
 
EFI_STATUS
EFIAPI
PlatformInitPreMemCallBack (
  IN CONST EFI_PEI_SERVICES      **PeiServices,
  IN EFI_PEI_NOTIFY_DESCRIPTOR   *NotifyDescriptor,
  IN VOID                        *Ppi
  );
 
EFI_STATUS
EFIAPI
MemoryDiscoveredPpiNotify (
  IN CONST EFI_PEI_SERVICES      **PeiServices,
  IN EFI_PEI_NOTIFY_DESCRIPTOR   *NotifyDescriptor,
  IN VOID                        *Ppi
  );
 
EFI_STATUS
EFIAPI
PchReset (
  IN CONST EFI_PEI_SERVICES    **PeiServices
  );
 
static EFI_PEI_RESET_PPI mResetPpi = {
  PchReset
};
 
static EFI_PEI_PPI_DESCRIPTOR mPreMemPpiList[] = {
  {
    (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
    &gEfiPeiResetPpiGuid,
    &mResetPpi
  }
};
 
static EFI_PEI_NOTIFY_DESCRIPTOR mPreMemNotifyList = {
  (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
  &gEfiPeiReadOnlyVariable2PpiGuid,
  (EFI_PEIM_NOTIFY_ENTRY_POINT)PlatformInitPreMemCallBack
};
 
static EFI_PEI_NOTIFY_DESCRIPTOR mMemDiscoveredNotifyList = {
  (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
  &gEfiPeiMemoryDiscoveredPpiGuid,
  (EFI_PEIM_NOTIFY_ENTRY_POINT)MemoryDiscoveredPpiNotify
};
 
/**
  Board misc init function for PEI pre-memory phase.
 
  @param[in]  BoardId       An unsigned integer represent the board id.
 
  @retval     EFI_SUCCESS   The function completed successfully.
**/
EFI_STATUS
BoardMiscInitPreMem (
  IN UINT16 BoardId
  )
{
  PCD64_BLOB PcdData;
 
  //
  // RecoveryMode GPIO
  //
  PcdData.Blob = 0;
  PcdData.BoardGpioConfig.Type = BoardGpioTypeNotSupported;
 
  switch (BoardId) {
    case BoardIdCometLakeULpddr3Rvp:
      PcdData.BoardGpioConfig.Type = BoardGpioTypePch;
      PcdData.BoardGpioConfig.u.Pin = GPIO_CNL_LP_GPP_F10;
    break;
 
    default:
      break;
  }
 
  //
  // Configure WWAN Full Card Power Off and reset pins
  //
  switch (BoardId) {
    case BoardIdCometLakeULpddr3Rvp:
      //
      // According to board default settings, GPP_D16 is used to enable/disable modem
      // power. An alternative way to contol modem power is to toggle FCP_OFF via GPP_D13
      // but board rework is required.
      //
      PcdSet32S (PcdWwanFullCardPowerOffGpio, GPIO_CNL_LP_GPP_D16);
      PcdSet32S (PcdWwanBbrstGpio, GPIO_CNL_LP_GPP_F1);
      PcdSet32S (PcdWwanPerstGpio, GPIO_CNL_LP_GPP_E15);
      PcdSet8S (PcdWwanPerstGpioPolarity, 1);
      break;
 
    default:
      break;
  }
 
  PcdSet64S (PcdRecoveryModeGpio, PcdData.Blob);
 
  //
  // Pc8374SioKbc Present
  //
  PcdSetBoolS (PcdPc8374SioKbcPresent, FALSE);
 
  return EFI_SUCCESS;
}
 
/**
  Board configuration initialization in the pre-memory boot phase.
**/
VOID
BoardConfigInitPreMem (
  VOID
  )
{
  EFI_STATUS Status;
  UINT16 BoardId;
 
  BoardId = BoardIdCometLakeULpddr3Rvp;
 
  Status = MrcConfigInit (BoardId);
  ASSERT_EFI_ERROR (Status);
 
  Status = SaGpioConfigInit (BoardId);
  ASSERT_EFI_ERROR (Status);
 
  Status = SaMiscConfigInit (BoardId);
  ASSERT_EFI_ERROR (Status);
 
  Status = RootPortClkInfoInit (BoardId);
  ASSERT_EFI_ERROR (Status);
 
  Status = UsbConfigInit (BoardId);
  ASSERT_EFI_ERROR (Status);
 
  Status = GpioGroupTierInit (BoardId);
  ASSERT_EFI_ERROR (Status);
 
  Status = GpioTablePreMemInit (BoardId);
  ASSERT_EFI_ERROR (Status);
 
  Status = PchPmConfigInit (BoardId);
  ASSERT_EFI_ERROR (Status);
 
  Status = BoardMiscInitPreMem (BoardId);
  ASSERT_EFI_ERROR (Status);
 
  Status = SaDisplayConfigInit (BoardId);
  ASSERT_EFI_ERROR (Status);
 
  Status = BoardFunctionInitPreMem (BoardId);
  ASSERT_EFI_ERROR (Status);
}
 
/**
  This function handles PlatformInit task after PeiReadOnlyVariable2 PPI produced
 
  @param[in]  PeiServices   Pointer to PEI Services Table.
  @param[in]  NotifyDesc    Pointer to the descriptor for the Notification event that
                            caused this function to execute.
  @param[in]  Ppi           Pointer to the PPI data associated with this function.
 
  @retval     EFI_SUCCESS  The function completes successfully
  @retval     others       Failure
**/
EFI_STATUS
EFIAPI
PlatformInitPreMemCallBack (
  IN CONST EFI_PEI_SERVICES     **PeiServices,
  IN EFI_PEI_NOTIFY_DESCRIPTOR  *NotifyDescriptor,
  IN VOID                       *Ppi
  )
{
  EFI_STATUS                        Status;
  UINT8                             FwConfig;
 
  //
  // Init Board Config Pcd.
  //
  BoardConfigInitPreMem ();
 
  DEBUG ((DEBUG_ERROR, "Fail to get System Configuration and set the configuration to production mode!\n"));
  FwConfig = FwConfigProduction;
  PcdSetBoolS (PcdPcieWwanEnable, FALSE);
  PcdSetBoolS (PcdWwanResetWorkaround, FALSE);
 
  //
  // Early Board Configuration before memory is ready.
  //
  Status = BoardInitEarlyPreMem ();
  ASSERT_EFI_ERROR (Status);
 
  ///
  /// If there was unexpected reset but no WDT expiration and no resume from S3/S4,
  /// clear unexpected reset status and enforce expiration. This is to inform Firmware
  /// which has no access to unexpected reset status bit, that something went wrong.
  ///
  OcWdtResetCheck ();
 
  Status = OcWdtInit ();
  ASSERT_EFI_ERROR (Status);
 
  //
  // Initialize Intel PEI Platform Policy
  //
  PeiPolicyInitPreMem (FwConfig);
 
  ///
  /// Configure GPIO and SIO
  ///
  Status = BoardInitPreMem ();
  ASSERT_EFI_ERROR (Status);
 
  ///
  /// Install Pre Memory PPIs
  ///
  Status = PeiServicesInstallPpi (&mPreMemPpiList[0]);
  ASSERT_EFI_ERROR (Status);
 
  return Status;
}
 
/**
  Provide hard reset PPI service.
  To generate full hard reset, write 0x0E to PCH RESET_GENERATOR_PORT (0xCF9).
 
  @param[in]  PeiServices       General purpose services available to every PEIM.
 
  @retval     Not return        System reset occured.
  @retval     EFI_DEVICE_ERROR  Device error, could not reset the system.
**/
EFI_STATUS
EFIAPI
PchReset (
  IN CONST EFI_PEI_SERVICES    **PeiServices
  )
{
  DEBUG ((DEBUG_INFO, "Perform Cold Reset\n"));
  IoWrite8 (RESET_GENERATOR_PORT, 0x0E);
 
  CpuDeadLoop ();
 
  ///
  /// System reset occured, should never reach at this line.
  ///
  ASSERT_EFI_ERROR (EFI_DEVICE_ERROR);
  return EFI_DEVICE_ERROR;
}
 
/**
  Install Firmware Volume Hob's once there is main memory
 
  @param[in]  PeiServices       General purpose services available to every PEIM.
  @param[in]  NotifyDescriptor  Notify that this module published.
  @param[in]  Ppi               PPI that was installed.
 
  @retval     EFI_SUCCESS       The function completed successfully.
**/
EFI_STATUS
EFIAPI
MemoryDiscoveredPpiNotify (
  IN CONST EFI_PEI_SERVICES     **PeiServices,
  IN EFI_PEI_NOTIFY_DESCRIPTOR  *NotifyDescriptor,
  IN VOID                       *Ppi
  )
{
  EFI_STATUS                    Status;
  EFI_BOOT_MODE                 BootMode;
  UINTN                         Index;
  UINT8                         PhysicalAddressBits;
  UINT32                        RegEax;
  MEMORY_MAP                    PcieMmioMap;
 
  Index = 0;
 
  Status = PeiServicesGetBootMode (&BootMode);
  ASSERT_EFI_ERROR (Status);
 
  AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
  if (RegEax >= 0x80000008) {
    AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);
    PhysicalAddressBits = (UINT8)RegEax;
  }
  else {
    PhysicalAddressBits = 36;
  }
 
  ///
  /// Create a CPU hand-off information
  ///
  BuildCpuHob (PhysicalAddressBits, 16);
 
  ///
  /// Build Memory Mapped IO Resource which is used to build E820 Table in LegacyBios.
  ///
  PcieMmioMap.BaseAddress = FixedPcdGet64 (PcdPciExpressBaseAddress);
  PcieMmioMap.Length = PcdGet32 (PcdPciExpressRegionLength);
 
  BuildResourceDescriptorHob (
    EFI_RESOURCE_MEMORY_MAPPED_IO,
     (EFI_RESOURCE_ATTRIBUTE_PRESENT      |
      EFI_RESOURCE_ATTRIBUTE_INITIALIZED  |
      EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE),
    PcieMmioMap.BaseAddress,
    PcieMmioMap.Length
    );
  BuildMemoryAllocationHob (
    PcieMmioMap.BaseAddress,
    PcieMmioMap.Length,
    EfiMemoryMappedIO
    );
  for (Index = 0; Index < sizeof(MmioMap) / (sizeof(MEMORY_MAP)); Index++) {
    BuildResourceDescriptorHob (
      EFI_RESOURCE_MEMORY_MAPPED_IO,
       (EFI_RESOURCE_ATTRIBUTE_PRESENT      |
        EFI_RESOURCE_ATTRIBUTE_INITIALIZED  |
        EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE),
      MmioMap[Index].BaseAddress,
      MmioMap[Index].Length
      );
    BuildMemoryAllocationHob (
      MmioMap[Index].BaseAddress,
      MmioMap[Index].Length,
      EfiMemoryMappedIO
      );
  }
 
  //
  // Report resource HOB for flash FV
  //
  BuildResourceDescriptorHob (
    EFI_RESOURCE_MEMORY_MAPPED_IO,
     (EFI_RESOURCE_ATTRIBUTE_PRESENT      |
      EFI_RESOURCE_ATTRIBUTE_INITIALIZED  |
      EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE),
    (UINTN) FixedPcdGet32 (PcdFlashAreaBaseAddress),
    (UINTN) FixedPcdGet32 (PcdFlashAreaSize)
    );
 
  BuildMemoryAllocationHob (
    (UINTN) FixedPcdGet32 (PcdFlashAreaBaseAddress),
    (UINTN) FixedPcdGet32 (PcdFlashAreaSize),
    EfiMemoryMappedIO
    );
 
  BuildFvHob (
    (UINTN)FixedPcdGet32 (PcdFlashAreaBaseAddress),
    (UINTN)FixedPcdGet32 (PcdFlashAreaSize)
    );
 
  return Status;
}
 
/**
  Board configuration init function for PEI pre-memory phase.
 
  @retval EFI_SUCCESS             The function completed successfully.
  @retval EFI_INVALID_PARAMETER   The parameter is NULL.
**/
EFI_STATUS
EFIAPI
CometlakeURvpInitPreMem (
  VOID
  )
{
  EFI_STATUS Status;
 
  ///
  /// Install Stall PPI
  ///
  Status = InstallStallPpi ();
  ASSERT_EFI_ERROR (Status);
 
  //
  // Install PCH RESET PPI and EFI RESET2 PeiService
  //
  Status = PchInitializeReset ();
  ASSERT_EFI_ERROR (Status);
 
  ///
  /// Performing PlatformInitPreMemCallBack after PeiReadOnlyVariable2 PPI produced
  ///
  Status = PeiServicesNotifyPpi (&mPreMemNotifyList);
 
  ///
  /// After code reorangized, memorycallback will run because the PPI is already
  /// installed when code run to here, it is supposed that the InstallEfiMemory is
  /// done before.
  ///
  Status = PeiServicesNotifyPpi (&mMemDiscoveredNotifyList);
 
  return EFI_SUCCESS;
}
 
/**
  Configure GPIO and SIO before memory ready
 
  @retval  EFI_SUCCESS   Operation success.
**/
EFI_STATUS
EFIAPI
CometlakeURvpBoardInitBeforeMemoryInit(
  VOID
  )
{
  ///
  /// Do basic PCH init
  ///
  SiliconInit ();
 
  CometlakeURvpInitPreMem();
 
  return EFI_SUCCESS;
}
 
EFI_STATUS
EFIAPI
CometlakeURvpBoardDebugInit(
  VOID
  )
{
  ///
  /// Do Early PCH init
  ///
  EarlySiliconInit ();
  return EFI_SUCCESS;
}
 
EFI_BOOT_MODE
EFIAPI
CometlakeURvpBoardBootModeDetect(
  VOID
  )
{
  return BOOT_WITH_FULL_CONFIGURATION;
}