Code scrub for Metronome driver.
[edk2.git] / MdeModulePkg / Universal / Metronome / Metronome.c
blobaceb9f558be3b8c2fe957b2bdbac882bec21dab7
1 /** @file
2 Produces the Metronome Architectural Protocol on top of Timer Library.
4 This is a generic implementation of the Metronome Architectural Protocol that
5 layers on top of an instance of the Timer Library. The Timer Library provides
6 functions for nanosecond and microsecond delays. This generic implementation
7 produces a fixed TickPeriod of 1 100ns unit, and when the WaitForTick() service
8 is called, the number of ticks passed in is converted to either nanosecond or
9 microsecond units. If the number of ticks is small, then nanoseconds are used.
10 If the number of ticks is large, then microseconds are used. This prevents
11 overflows that could occur for long delays if only nanoseconds were used and also
12 provides the greatest accuracy for small delays.
14 Copyright (c) 2008, Intel Corporation
15 All rights reserved. This program and the accompanying materials
16 are licensed and made available under the terms and conditions of the BSD License
17 which accompanies this distribution. The full text of the license may be found at
18 http://opensource.org/licenses/bsd-license.php
20 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
21 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
23 **/
25 #include "Metronome.h"
28 // Handle for the Metronome Architectural Protocol instance produced by this driver
30 EFI_HANDLE mMetronomeHandle = NULL;
33 // The Metronome Architectural Protocol instance produced by this driver
35 EFI_METRONOME_ARCH_PROTOCOL mMetronome = {
36 WaitForTick,
37 1 // TickPeriod = 1*100 ns units
40 /**
41 Waits for the specified number of ticks.
43 This function implements EFI_METRONOME_ARCH_PROTOCOL.WaitForTick().
44 The WaitForTick() function waits for the number of ticks specified by
45 TickNumber from a known time source in the platform. If TickNumber of
46 ticks are detected, then EFI_SUCCESS is returned. The actual time passed
47 between entry of this function and the first tick is between 0 and
48 TickPeriod 100 nS units. If you want to guarantee that at least TickPeriod
49 time has elapsed, wait for two ticks. This function waits for a hardware
50 event to determine when a tick occurs. It is possible for interrupt
51 processing, or exception processing to interrupt the execution of the
52 WaitForTick() function. Depending on the hardware source for the ticks, it
53 is possible for a tick to be missed. This function cannot guarantee that
54 ticks will not be missed. If a timeout occurs waiting for the specified
55 number of ticks, then EFI_TIMEOUT is returned.
57 @param This The EFI_METRONOME_ARCH_PROTOCOL instance.
58 @param TickNumber Number of ticks to wait.
60 @retval EFI_SUCCESS The wait for the number of ticks specified by TickNumber
61 succeeded.
62 @retval EFI_TIMEOUT A timeout occurred waiting for the specified number of ticks.
64 **/
65 EFI_STATUS
66 EFIAPI
67 WaitForTick (
68 IN EFI_METRONOME_ARCH_PROTOCOL *This,
69 IN UINT32 TickNumber
73 // Check the value of TickNumber, so a 32-bit overflow can be avoided
74 // when TickNumber of converted to nanosecond units
76 if (TickNumber < 10000000) {
78 // If TickNumber is small, then use NanoSecondDelay()
80 NanoSecondDelay (TickNumber * 100);
81 } else {
83 // If TickNumber is large, then use MicroSecondDelay()
85 MicroSecondDelay (TickNumber / 10);
87 return EFI_SUCCESS;
90 /**
91 The user Entry Point for module Metronome. The user code starts with this function.
93 @param[in] ImageHandle The firmware allocated handle for the EFI image.
94 @param[in] SystemTable A pointer to the EFI System Table.
96 @retval EFI_SUCCESS The entry point is executed successfully.
97 @retval other Some error occurs when executing this entry point.
99 **/
100 EFI_STATUS
101 EFIAPI
102 InstallMetronome (
103 IN EFI_HANDLE ImageHandle,
104 IN EFI_SYSTEM_TABLE *SystemTable
107 EFI_STATUS Status;
110 // Make sure the Metronome Architectural Protocol is not already installed in the system
112 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiMetronomeArchProtocolGuid);
115 // Install on a new handle
117 Status = gBS->InstallMultipleProtocolInterfaces (
118 &mMetronomeHandle,
119 &gEfiMetronomeArchProtocolGuid, &mMetronome,
120 NULL
122 ASSERT_EFI_ERROR (Status);
124 return Status;