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