Moving OMAP 3530 code out of BeagleBoard package into its own package
[mirror_edk2.git] / Omap35xxPkg / Library / BeagleBoardTimerLib / TimerLib.c
1 /** @file
2
3 Copyright (c) 2008-2009, Apple Inc. All rights reserved.
4
5 All rights reserved. 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 <Base.h>
16
17 #include <Library/BaseLib.h>
18 #include <Library/TimerLib.h>
19 #include <Library/DebugLib.h>
20 #include <Library/PcdLib.h>
21 #include <Library/IoLib.h>
22 #include <Library/OmapLib.h>
23
24 #include <Omap3530/Omap3530.h>
25
26 UINTN
27 EFIAPI
28 MicroSecondDelay (
29 IN UINTN MicroSeconds
30 )
31 {
32 UINT64 NanoSeconds;
33
34 NanoSeconds = MultU64x32(MicroSeconds, 1000);
35
36 while (NanoSeconds > (UINTN)-1) {
37 NanoSecondDelay((UINTN)-1);
38 NanoSeconds -= (UINTN)-1;
39 }
40
41 NanoSecondDelay(NanoSeconds);
42
43 return MicroSeconds;
44 }
45
46 UINTN
47 EFIAPI
48 NanoSecondDelay (
49 IN UINTN NanoSeconds
50 )
51 {
52 UINT32 Delay;
53 UINT32 StartTime;
54 UINT32 CurrentTime;
55 UINT32 ElapsedTime;
56 UINT32 TimerCountRegister;
57
58 Delay = (NanoSeconds / PcdGet32(PcdEmbeddedFdPerformanceCounterPeriodInNanoseconds)) + 1;
59
60 TimerCountRegister = TimerBase(PcdGet32(PcdBeagleFreeTimer)) + GPTIMER_TCRR;
61
62 StartTime = MmioRead32(TimerCountRegister);
63
64 do
65 {
66 CurrentTime = MmioRead32(TimerCountRegister);
67 ElapsedTime = CurrentTime - StartTime;
68 } while (ElapsedTime < Delay);
69
70 NanoSeconds = ElapsedTime * PcdGet32(PcdEmbeddedFdPerformanceCounterPeriodInNanoseconds);
71
72 return NanoSeconds;
73 }
74
75 UINT64
76 EFIAPI
77 GetPerformanceCounter (
78 VOID
79 )
80 {
81 return (UINT64)MmioRead32(TimerBase(PcdGet32(PcdBeagleFreeTimer)) + GPTIMER_TCRR);
82 }
83
84 UINT64
85 EFIAPI
86 GetPerformanceCounterProperties (
87 OUT UINT64 *StartValue, OPTIONAL
88 OUT UINT64 *EndValue OPTIONAL
89 )
90 {
91 if (StartValue != NULL) {
92 // Timer starts with the reload value
93 *StartValue = (UINT64)MmioRead32(TimerBase(PcdGet32(PcdBeagleFreeTimer)) + GPTIMER_TLDR);
94 }
95
96 if (EndValue != NULL) {
97 // Timer counts up to 0xFFFFFFFF
98 *EndValue = 0xFFFFFFFF;
99 }
100
101 return PcdGet64(PcdEmbeddedPerformanceCounterFreqencyInHz);
102 }