summaryrefslogtreecommitdiff
path: root/Silicon/Intel/LewisburgPkg/LibraryPrivate/BasePchResetCommonLib/PchResetCommon.c
blob: 7b3c894700c1d459893288ab0b3b4ec11153b80f (plain)
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
/** @file

Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials are licensed and made available under
the terms and conditions of the BSD License that accompanies this distribution.
The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php.

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include <Uefi/UefiBaseType.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <Library/IoLib.h>
#include <PchAccess.h>
#include <Library/PchCycleDecodingLib.h>
#include <Library/MmPciBaseLib.h>
#include <IncludePrivate/Library/PchResetCommonLib.h>


EFI_STATUS
EFIAPI
PchResetCallback (
  IN     PCH_RESET_TYPE           PchResetType
  );

/**
  Initialize an Pch Reset ppi/protocol instance.

  @param[in] PchResetInstance     Pointer to PchResetInstance to initialize

  @retval EFI_SUCCESS             The protocol instance was properly initialized
  @exception EFI_UNSUPPORTED      The PCH is not supported by this module
**/
EFI_STATUS
PchResetConstructor (
  PCH_RESET_INSTANCE          *PchResetInstance
  )
{
  UINTN   PmcBaseAddress;

  ///
  /// Initialize the Reset protocol instance
  ///
  PchResetInstance->Signature               = PCH_RESET_SIGNATURE;
  PchResetInstance->Handle                  = NULL;

  ///
  /// Sanity check to ensure PMC ACPI/PM BASE initialization has occurred previously.
  ///
  PmcBaseAddress = MmPciBase (
                     DEFAULT_PCI_BUS_NUMBER_PCH,
                     PCI_DEVICE_NUMBER_PCH_PMC,
                     PCI_FUNCTION_NUMBER_PCH_PMC
                     );
  PchResetInstance->PchPmcBase = PmcBaseAddress;
  PchPwrmBaseGet (&(PchResetInstance->PchPwrmBase));
  ASSERT (PchResetInstance->PchPwrmBase != 0);
  PchAcpiBaseGet (&(PchResetInstance->PchAcpiBase));
  ASSERT (PchResetInstance->PchAcpiBase != 0);


  return EFI_SUCCESS;
}

/**
  Execute Pch Reset from the host controller.
  @param[in] PchResetInstance     Pointer to PchResetInstance to initialize
  @param[in] PchResetType         Pch Reset Types which includes ColdReset, WarmReset, ShutdownReset,
                                  PowerCycleReset, GlobalReset, GlobalResetWithEc

  @retval EFI_SUCCESS             Successfully completed.
  @retval EFI_INVALID_PARAMETER   If ResetType is invalid.
**/
EFI_STATUS
PchReset (
  IN  PCH_RESET_INSTANCE *PchResetInstance,
  IN  PCH_RESET_TYPE     PchResetType
  )
{
  UINTN      PmcBaseAddress;
  UINT16     ABase;
  UINT8      OutputData;
  UINT32     Data32;
  UINT16     Data16;
  EFI_STATUS Status;

  PmcBaseAddress = PchResetInstance->PchPmcBase;
  ABase          = PchResetInstance->PchAcpiBase;
  switch (PchResetType) {
    case ColdReset:
      IoWrite8 ((UINTN) R_PCH_RST_CNT, (UINT8) V_PCH_RST_CNT_HARDSTARTSTATE);
      OutputData = V_PCH_RST_CNT_FULLRESET;
      break;

    case WarmReset:
      IoWrite8 ((UINTN) R_PCH_RST_CNT, (UINT8) V_PCH_RST_CNT_SOFTSTARTSTATE);
      OutputData = V_PCH_RST_CNT_HARDRESET;
      break;

    case ShutdownReset:
      ///
      /// Firstly, ACPI decode must be enabled
      ///
      MmioOr8 (
        PmcBaseAddress + R_PCH_PMC_ACPI_CNT,
        (UINT8) (B_PCH_PMC_ACPI_CNT_ACPI_EN)
        );

      ///
      /// Then, GPE0_EN should be disabled to avoid any GPI waking up the system from S5
      ///
      IoWrite32 ((UINTN) (ABase + R_PCH_ACPI_GPE0_EN_127_96), 0);
      
      ///
      /// Secondly, PwrSts register must be cleared
      ///
      /// Write a "1" to bit[8] of power button status register at
      /// (PM_BASE + PM1_STS_OFFSET) to clear this bit
      ///
      Data16 = B_PCH_SMI_STS_PM1_STS_REG;
      IoWrite16 ((UINTN) (ABase + R_PCH_SMI_STS), Data16);
      
      ///
      /// Finally, transform system into S5 sleep state
      ///
      Data32  = IoRead32 ((UINTN) (ABase + R_PCH_ACPI_PM1_CNT));
      
      Data32  = (UINT32) ((Data32 &~(B_PCH_ACPI_PM1_CNT_SLP_TYP + B_PCH_ACPI_PM1_CNT_SLP_EN)) | V_PCH_ACPI_PM1_CNT_S5);
      
      IoWrite32 ((UINTN) (ABase + R_PCH_ACPI_PM1_CNT), Data32);
      
      Data32 = Data32 | B_PCH_ACPI_PM1_CNT_SLP_EN;
      
      IoWrite32 ((UINTN) (ABase + R_PCH_ACPI_PM1_CNT), Data32);
      return EFI_SUCCESS;

    case PowerCycleReset:
    case GlobalReset:
    case GlobalResetWithEc:
      ///
      /// PCH BIOS Spec Section 4.6 GPIO Reset Requirement
      ///

      if ((PchResetType == GlobalReset) || (PchResetType == GlobalResetWithEc)) {
        MmioOr32 (
          PmcBaseAddress + R_PCH_PMC_ETR3,
          (UINT32) (B_PCH_PMC_ETR3_CF9GR)
          );
      }
      OutputData = V_PCH_RST_CNT_FULLRESET;
      break;

    default:
      return EFI_INVALID_PARAMETER;
  }

  DEBUG ((DEBUG_ERROR, "Resetting the platform (%02x)...\n", OutputData));

  Status = PchResetCallback (PchResetType);

  if ((Status == EFI_SUCCESS) || (Status == EFI_NOT_FOUND)) {
    IoWrite8 ((UINTN) R_PCH_RST_CNT, OutputData);
    ///
    /// Waiting for system reset
    ///
    CpuDeadLoop ();
  }

  return Status;
}