gt.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*++
  2. Copyright (c) 2016 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. gt.c
  5. Abstract:
  6. This module implements timer support for the ARM Generic Timer.
  7. Author:
  8. Chris Stevens 9-Jun-2016
  9. Environment:
  10. Firmware
  11. --*/
  12. //
  13. // ------------------------------------------------------------------- Includes
  14. //
  15. #include <uefifw.h>
  16. #include "dev/gt.h"
  17. //
  18. // ---------------------------------------------------------------- Definitions
  19. //
  20. //
  21. // Define the bits for a generic timer control register.
  22. //
  23. #define GT_CONTROL_INTERRUPT_STATUS_ASSERTED 0x00000004
  24. #define GT_CONTROL_INTERRUPT_MASKED 0x00000002
  25. #define GT_CONTROL_TIMER_ENABLE 0x00000001
  26. //
  27. // --------------------------------------------------------------------- Macros
  28. //
  29. //
  30. // ----------------------------------------------- Internal Function Prototypes
  31. //
  32. VOID
  33. EfipGtSetVirtualTimerControl (
  34. UINT32 Control
  35. );
  36. UINT64
  37. EfipGtGetVirtualCount (
  38. VOID
  39. );
  40. VOID
  41. EfipGtSetVirtualTimerCompare (
  42. UINT64 CompareValue
  43. );
  44. //
  45. // ------------------------------------------------------ Data Type Definitions
  46. //
  47. //
  48. // -------------------------------------------------------------------- Globals
  49. //
  50. //
  51. // ------------------------------------------------------------------ Functions
  52. //
  53. EFI_STATUS
  54. EfipGtInitialize (
  55. PGT_CONTEXT Context
  56. )
  57. /*++
  58. Routine Description:
  59. This routine initializes an ARM Generic Timer.
  60. Arguments:
  61. Context - Supplies a pointer to the timer context.
  62. Return Value:
  63. EFI Status code.
  64. --*/
  65. {
  66. //
  67. // The timer is already running, just make sure interrupts are off.
  68. //
  69. Context->Period = 0;
  70. EfipGtSetVirtualTimerControl(0);
  71. return EFI_SUCCESS;
  72. }
  73. UINT64
  74. EfipGtRead (
  75. PGT_CONTEXT Context
  76. )
  77. /*++
  78. Routine Description:
  79. This routine returns the hardware counter's raw value.
  80. Arguments:
  81. Context - Supplies a pointer to the timer context.
  82. Return Value:
  83. Returns the timer's current count.
  84. --*/
  85. {
  86. return EfipGtGetVirtualCount();
  87. }
  88. EFI_STATUS
  89. EfipGtArm (
  90. PGT_CONTEXT Context,
  91. BOOLEAN Periodic,
  92. UINT64 TickCount
  93. )
  94. /*++
  95. Routine Description:
  96. This routine arms the timer to fire an interrupt after the specified number
  97. of ticks.
  98. Arguments:
  99. Context - Supplies a pointer to the timer context.
  100. Periodic - Supplies a boolean indicating whether or not the timer should
  101. interrupt periodically or just once.
  102. TickCount - Supplies the number of timer ticks from now for the timer to
  103. fire in.
  104. Return Value:
  105. EFI Status code.
  106. --*/
  107. {
  108. UINT64 DueTime;
  109. BOOLEAN Enabled;
  110. //
  111. // In order to synchronize with the rearming of the timer during
  112. // acknowledge interrupt, perform the arm with interrupts disabled.
  113. //
  114. Enabled = EfiDisableInterrupts();
  115. //
  116. // The tick count is relative in both modes, but the GT can only be
  117. // armed with an absolute time. Add the current time.
  118. //
  119. DueTime = TickCount + EfipGtGetVirtualCount();
  120. if (Periodic != FALSE) {
  121. Context->Period = TickCount;
  122. Context->DueTime = DueTime;
  123. } else {
  124. Context->Period = 0;
  125. }
  126. EfipGtSetVirtualTimerCompare(DueTime);
  127. EfipGtSetVirtualTimerControl(GT_CONTROL_TIMER_ENABLE);
  128. if (Enabled != FALSE) {
  129. EfiEnableInterrupts();
  130. }
  131. return EFI_SUCCESS;
  132. }
  133. VOID
  134. EfipGtDisarm (
  135. PGT_CONTEXT Context
  136. )
  137. /*++
  138. Routine Description:
  139. This routine disarms the timer, stopping interrupts from firing.
  140. Arguments:
  141. Context - Supplies a pointer to the timer context.
  142. Return Value:
  143. None.
  144. --*/
  145. {
  146. BOOLEAN Enabled;
  147. //
  148. // In order to synchronize with the rearming of the timer during
  149. // acknowledge interrupt, perform the disarm with interrupts disabled.
  150. //
  151. Enabled = EfiDisableInterrupts();
  152. Context->Period = 0;
  153. EfipGtSetVirtualTimerControl(0);
  154. if (Enabled != FALSE) {
  155. EfiEnableInterrupts();
  156. }
  157. return;
  158. }
  159. VOID
  160. EfipGtAcknowledgeInterrupt (
  161. PGT_CONTEXT Context
  162. )
  163. /*++
  164. Routine Description:
  165. This routine performs any actions necessary upon reciept of a timer's
  166. interrupt. This may involve writing to an acknowledge register to re-enable
  167. the timer to fire again, or other hardware specific actions.
  168. Arguments:
  169. Context - Supplies a pointer to the timer context.
  170. Return Value:
  171. None.
  172. --*/
  173. {
  174. UINT64 DueTime;
  175. //
  176. // The only way to stop an interrupt from continuing to fire is to either
  177. // reprogram the compare register or to disable the interrupt. As the timer
  178. // must await further instruction, disable the interrupt.
  179. //
  180. EfipGtSetVirtualTimerControl(0);
  181. if (Context->Period != 0) {
  182. DueTime = Context->DueTime + Context->Period;
  183. Context->DueTime = DueTime;
  184. EfipGtSetVirtualTimerCompare(DueTime);
  185. EfipGtSetVirtualTimerControl(GT_CONTROL_TIMER_ENABLE);
  186. }
  187. return;
  188. }
  189. //
  190. // --------------------------------------------------------- Internal Functions
  191. //