1/** @file
2
3  Copyright (c) 2004  - 2014, Intel Corporation. All rights reserved.<BR>
4
5
6  This program and the accompanying materials are licensed and made available under
7
8  the terms and conditions of the BSD License that accompanies this distribution.
9
10  The full text of the license may be found at
11
12  http://opensource.org/licenses/bsd-license.php.
13
14
15
16  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
17
18  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19
20
21
22
23
24Module Name:
25
26  Stall.c
27
28Abstract:
29
30  Produce Stall Ppi.
31
32--*/
33
34
35#include "CommonHeader.h"
36#include "PlatformBaseAddresses.h"
37#include "PchRegs.h"
38
39/**
40  Waits for at least the given number of microseconds.
41
42  @param PeiServices     General purpose services available to every PEIM.
43  @param This            PPI instance structure.
44  @param Microseconds    Desired length of time to wait.
45
46  @retval EFI_SUCCESS    If the desired amount of time was passed.
47
48**/
49EFI_STATUS
50EFIAPI
51Stall (
52  IN CONST EFI_PEI_SERVICES   **PeiServices,
53  IN CONST EFI_PEI_STALL_PPI      *This,
54  IN UINTN              Microseconds
55  )
56{
57  UINTN   Ticks;
58  UINTN   Counts;
59  UINT32  CurrentTick;
60  UINT32  OriginalTick;
61  UINT32  RemainingTick;
62
63  if (Microseconds == 0) {
64    return EFI_SUCCESS;
65  }
66
67  OriginalTick = IoRead32 (ACPI_BASE_ADDRESS + R_PCH_ACPI_PM1_TMR);
68  OriginalTick &= (V_PCH_ACPI_PM1_TMR_MAX_VAL - 1);
69  CurrentTick = OriginalTick;
70
71  //
72  // The timer frequency is 3.579545MHz, so 1 ms corresponds to 3.58 clocks
73  //
74  Ticks = Microseconds * 358 / 100 + OriginalTick + 1;
75
76  //
77  // The loops needed for timer overflow
78  //
79  Counts = (UINTN)RShiftU64((UINT64)Ticks, 24);
80
81  //
82  // Remaining clocks within one loop
83  //
84  RemainingTick = Ticks & 0xFFFFFF;
85
86  //
87  // Do not intend to use TMROF_STS bit of register PM1_STS, because this add extra
88  // one I/O operation, and may generate SMI
89  //
90  while (Counts != 0) {
91    CurrentTick = IoRead32 (ACPI_BASE_ADDRESS + R_PCH_ACPI_PM1_TMR) & B_PCH_ACPI_PM1_TMR_VAL;
92    if (CurrentTick <= OriginalTick) {
93      Counts--;
94    }
95    OriginalTick = CurrentTick;
96  }
97
98  while ((RemainingTick > CurrentTick) && (OriginalTick <= CurrentTick)) {
99    OriginalTick  = CurrentTick;
100    CurrentTick   = IoRead32 (ACPI_BASE_ADDRESS + R_PCH_ACPI_PM1_TMR) & B_PCH_ACPI_PM1_TMR_VAL;
101  }
102
103  return EFI_SUCCESS;
104}
105