]> git.proxmox.com Git - mirror_edk2.git/blob - Vlv2TbltDevicePkg/PlatformInitPei/Stall.c
Vlv2TbltDevicePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / Vlv2TbltDevicePkg / PlatformInitPei / Stall.c
1 /** @file
2
3 Copyright (c) 2004 - 2014, Intel Corporation. All rights reserved.<BR>
4
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7
8
9
10
11 Module Name:
12
13 Stall.c
14
15 Abstract:
16
17 Produce Stall Ppi.
18
19 --*/
20
21
22 #include "PlatformEarlyInit.h"
23
24
25 /**
26
27 Waits for at least the given number of microseconds.
28
29 @param PeiServices General purpose services available to every PEIM.
30 @param This PPI instance structure.
31 @param Microseconds Desired length of time to wait.
32
33 @retval EFI_SUCCESS If the desired amount of time was passed.
34
35 */
36 EFI_STATUS
37 EFIAPI
38 Stall (
39 IN CONST EFI_PEI_SERVICES **PeiServices,
40 IN CONST EFI_PEI_STALL_PPI *This,
41 IN UINTN Microseconds
42 )
43 {
44 UINTN Ticks;
45 UINTN Counts;
46 UINT32 CurrentTick;
47 UINT32 OriginalTick;
48 UINT32 RemainingTick;
49
50 if (Microseconds == 0) {
51 return EFI_SUCCESS;
52 }
53
54 OriginalTick = IoRead32 (ACPI_BASE_ADDRESS + R_PCH_ACPI_PM1_TMR);
55 OriginalTick &= (V_PCH_ACPI_PM1_TMR_MAX_VAL - 1);
56 CurrentTick = OriginalTick;
57
58 //
59 // The timer frequency is 3.579545MHz, so 1 ms corresponds to 3.58 clocks
60 //
61 Ticks = Microseconds * 358 / 100 + OriginalTick + 1;
62
63 //
64 // The loops needed for timer overflow
65 //
66 Counts = (UINTN)RShiftU64((UINT64)Ticks, 24);
67
68 //
69 // Remaining clocks within one loop
70 //
71 RemainingTick = Ticks & 0xFFFFFF;
72
73 //
74 // Do not intend to use TMROF_STS bit of register PM1_STS, because this add extra
75 // one I/O operation, and may generate SMI
76 //
77 while (Counts != 0) {
78 CurrentTick = IoRead32 (ACPI_BASE_ADDRESS + R_PCH_ACPI_PM1_TMR) & B_PCH_ACPI_PM1_TMR_VAL;
79 if (CurrentTick <= OriginalTick) {
80 Counts--;
81 }
82 OriginalTick = CurrentTick;
83 }
84
85 while ((RemainingTick > CurrentTick) && (OriginalTick <= CurrentTick)) {
86 OriginalTick = CurrentTick;
87 CurrentTick = IoRead32 (ACPI_BASE_ADDRESS + R_PCH_ACPI_PM1_TMR) & B_PCH_ACPI_PM1_TMR_VAL;
88 }
89
90 return EFI_SUCCESS;
91 }