TimerLib.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /** @file
  2. Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. **/
  5. #include <Uefi.h>
  6. #include <Library/BaseLib.h>
  7. #include <Library/TimerLib.h>
  8. #include <Library/DebugLib.h>
  9. #include <Library/PcdLib.h>
  10. #include <Library/IoLib.h>
  11. #include <Library/OmapLib.h>
  12. #include <Omap3530/Omap3530.h>
  13. RETURN_STATUS
  14. EFIAPI
  15. TimerConstructor (
  16. VOID
  17. )
  18. {
  19. UINTN Timer = PcdGet32(PcdOmap35xxFreeTimer);
  20. UINT32 TimerBaseAddress = TimerBase(Timer);
  21. if ((MmioRead32 (TimerBaseAddress + GPTIMER_TCLR) & TCLR_ST_ON) == 0) {
  22. // Set source clock for GPT3 & GPT4 to SYS_CLK
  23. MmioOr32 (CM_CLKSEL_PER, CM_CLKSEL_PER_CLKSEL_GPT3_SYS | CM_CLKSEL_PER_CLKSEL_GPT4_SYS);
  24. // Set count & reload registers
  25. MmioWrite32 (TimerBaseAddress + GPTIMER_TCRR, 0x00000000);
  26. MmioWrite32 (TimerBaseAddress + GPTIMER_TLDR, 0x00000000);
  27. // Disable interrupts
  28. MmioWrite32 (TimerBaseAddress + GPTIMER_TIER, TIER_TCAR_IT_DISABLE | TIER_OVF_IT_DISABLE | TIER_MAT_IT_DISABLE);
  29. // Start Timer
  30. MmioWrite32 (TimerBaseAddress + GPTIMER_TCLR, TCLR_AR_AUTORELOAD | TCLR_ST_ON);
  31. // Disable OMAP Watchdog timer (WDT2)
  32. MmioWrite32 (WDTIMER2_BASE + WSPR, 0xAAAA);
  33. DEBUG ((EFI_D_ERROR, "Magic delay to disable watchdog timers properly.\n"));
  34. MmioWrite32 (WDTIMER2_BASE + WSPR, 0x5555);
  35. }
  36. return EFI_SUCCESS;
  37. }
  38. UINTN
  39. EFIAPI
  40. MicroSecondDelay (
  41. IN UINTN MicroSeconds
  42. )
  43. {
  44. UINT64 NanoSeconds;
  45. NanoSeconds = MultU64x32(MicroSeconds, 1000);
  46. while (NanoSeconds > (UINTN)-1) {
  47. NanoSecondDelay((UINTN)-1);
  48. NanoSeconds -= (UINTN)-1;
  49. }
  50. NanoSecondDelay(NanoSeconds);
  51. return MicroSeconds;
  52. }
  53. UINTN
  54. EFIAPI
  55. NanoSecondDelay (
  56. IN UINTN NanoSeconds
  57. )
  58. {
  59. UINT32 Delay;
  60. UINT32 StartTime;
  61. UINT32 CurrentTime;
  62. UINT32 ElapsedTime;
  63. UINT32 TimerCountRegister;
  64. Delay = (NanoSeconds / PcdGet32(PcdEmbeddedPerformanceCounterPeriodInNanoseconds)) + 1;
  65. TimerCountRegister = TimerBase(PcdGet32(PcdOmap35xxFreeTimer)) + GPTIMER_TCRR;
  66. StartTime = MmioRead32 (TimerCountRegister);
  67. do
  68. {
  69. CurrentTime = MmioRead32 (TimerCountRegister);
  70. ElapsedTime = CurrentTime - StartTime;
  71. } while (ElapsedTime < Delay);
  72. NanoSeconds = ElapsedTime * PcdGet32(PcdEmbeddedPerformanceCounterPeriodInNanoseconds);
  73. return NanoSeconds;
  74. }
  75. UINT64
  76. EFIAPI
  77. GetPerformanceCounter (
  78. VOID
  79. )
  80. {
  81. return (UINT64)MmioRead32 (TimerBase(PcdGet32(PcdOmap35xxFreeTimer)) + GPTIMER_TCRR);
  82. }
  83. UINT64
  84. EFIAPI
  85. GetPerformanceCounterProperties (
  86. OUT UINT64 *StartValue, OPTIONAL
  87. OUT UINT64 *EndValue OPTIONAL
  88. )
  89. {
  90. if (StartValue != NULL) {
  91. // Timer starts with the reload value
  92. *StartValue = (UINT64)MmioRead32 (TimerBase(PcdGet32(PcdOmap35xxFreeTimer)) + GPTIMER_TLDR);
  93. }
  94. if (EndValue != NULL) {
  95. // Timer counts up to 0xFFFFFFFF
  96. *EndValue = 0xFFFFFFFF;
  97. }
  98. return PcdGet64(PcdEmbeddedPerformanceCounterFrequencyInHz);
  99. }
  100. /**
  101. Converts elapsed ticks of performance counter to time in nanoseconds.
  102. This function converts the elapsed ticks of running performance counter to
  103. time value in unit of nanoseconds.
  104. @param Ticks The number of elapsed ticks of running performance counter.
  105. @return The elapsed time in nanoseconds.
  106. **/
  107. UINT64
  108. EFIAPI
  109. GetTimeInNanoSecond (
  110. IN UINT64 Ticks
  111. )
  112. {
  113. UINT32 Period;
  114. Period = PcdGet32 (PcdEmbeddedPerformanceCounterPeriodInNanoseconds);
  115. return (Ticks * Period);
  116. }