1/** @file
2  Reset System Library functions for coreboot
3
4  Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
5  This program and the accompanying materials
6  are licensed and made available under the terms and conditions of the BSD License
7  which accompanies this distribution.  The full text of the license may be found at
8  http://opensource.org/licenses/bsd-license.php
9
10  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13**/
14
15#include <PiDxe.h>
16#include <Library/BaseLib.h>
17#include <Library/DebugLib.h>
18#include <Library/IoLib.h>
19#include <Library/HobLib.h>
20
21#include <Guid/AcpiBoardInfoGuid.h>
22
23VOID
24AcpiPmControl (
25  UINTN SuspendType
26  )
27{
28	EFI_HOB_GUID_TYPE  *GuidHob;
29	ACPI_BOARD_INFO    *pAcpiBoardInfo;
30	UINTN PmCtrlReg = 0;
31
32  ASSERT (SuspendType <= 7);
33  //
34	// Find the acpi board information guid hob
35	//
36	GuidHob = GetFirstGuidHob (&gUefiAcpiBoardInfoGuid);
37	ASSERT (GuidHob != NULL);
38  pAcpiBoardInfo = (ACPI_BOARD_INFO *)GET_GUID_HOB_DATA (GuidHob);
39
40  PmCtrlReg = (UINTN)pAcpiBoardInfo->PmCtrlRegBase;
41  IoAndThenOr16 (PmCtrlReg, (UINT16) ~0x3c00, (UINT16) (SuspendType << 10));
42  IoOr16 (PmCtrlReg, BIT13);
43  CpuDeadLoop ();
44}
45
46/**
47  Calling this function causes a system-wide reset. This sets
48  all circuitry within the system to its initial state. This type of reset
49  is asynchronous to system operation and operates without regard to
50  cycle boundaries.
51
52  System reset should not return, if it returns, it means the system does
53  not support cold reset.
54**/
55VOID
56EFIAPI
57ResetCold (
58  VOID
59  )
60{
61  EFI_HOB_GUID_TYPE  *GuidHob;
62	ACPI_BOARD_INFO    *pAcpiBoardInfo;
63
64	//
65	// Find the acpi board information guid hob
66	//
67	GuidHob = GetFirstGuidHob (&gUefiAcpiBoardInfoGuid);
68	ASSERT (GuidHob != NULL);
69  pAcpiBoardInfo = (ACPI_BOARD_INFO *)GET_GUID_HOB_DATA (GuidHob);
70
71  IoWrite8 ((UINTN)pAcpiBoardInfo->ResetRegAddress, pAcpiBoardInfo->ResetValue);
72  CpuDeadLoop ();
73}
74
75/**
76  Calling this function causes a system-wide initialization. The processors
77  are set to their initial state, and pending cycles are not corrupted.
78
79  System reset should not return, if it returns, it means the system does
80  not support warm reset.
81**/
82VOID
83EFIAPI
84ResetWarm (
85  VOID
86  )
87{
88	EFI_HOB_GUID_TYPE  *GuidHob;
89	ACPI_BOARD_INFO    *pAcpiBoardInfo;
90
91	//
92	// Find the acpi board information guid hob
93	//
94	GuidHob = GetFirstGuidHob (&gUefiAcpiBoardInfoGuid);
95	ASSERT (GuidHob != NULL);
96  pAcpiBoardInfo = (ACPI_BOARD_INFO *)GET_GUID_HOB_DATA (GuidHob);
97
98  IoWrite8 ((UINTN)pAcpiBoardInfo->ResetRegAddress, pAcpiBoardInfo->ResetValue);
99  CpuDeadLoop ();
100}
101
102/**
103  Calling this function causes the system to enter a power state equivalent
104  to the ACPI G2/S5 or G3 states.
105
106  System shutdown should not return, if it returns, it means the system does
107  not support shut down reset.
108**/
109VOID
110EFIAPI
111ResetShutdown (
112  VOID
113  )
114{
115  EFI_HOB_GUID_TYPE  *GuidHob;
116  ACPI_BOARD_INFO    *pAcpiBoardInfo;
117  UINTN              PmCtrlReg;
118
119  //
120  // Find the acpi board information guid hob
121  //
122  GuidHob = GetFirstGuidHob (&gUefiAcpiBoardInfoGuid);
123  ASSERT (GuidHob != NULL);
124  pAcpiBoardInfo = (ACPI_BOARD_INFO *)GET_GUID_HOB_DATA (GuidHob);
125
126  //
127  // GPE0_EN should be disabled to avoid any GPI waking up the system from S5
128  //
129  IoWrite16 ((UINTN)pAcpiBoardInfo->PmGpeEnBase,  0);
130
131  //
132  // Clear Power Button Status
133  //
134  IoWrite16((UINTN) pAcpiBoardInfo->PmEvtBase, BIT8);
135
136  //
137  // Transform system into S5 sleep state
138  //
139  PmCtrlReg = (UINTN)pAcpiBoardInfo->PmCtrlRegBase;
140  IoAndThenOr16 (PmCtrlReg, (UINT16) ~0x3c00, (UINT16) (7 << 10));
141  IoOr16 (PmCtrlReg, BIT13);
142  CpuDeadLoop ();
143
144  ASSERT (FALSE);
145}
146
147/**
148  Calling this function causes the system to enter a power state for capsule
149  update.
150
151  Reset update should not return, if it returns, it means the system does
152  not support capsule update.
153
154**/
155VOID
156EFIAPI
157EnterS3WithImmediateWake (
158  VOID
159  )
160{
161  AcpiPmControl (5);
162  ASSERT (FALSE);
163}
164