Timer.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /** @file
  2. Template for Timer Architecture Protocol driver of the ARM flavor
  3. Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <PiDxe.h>
  7. #include <Library/BaseLib.h>
  8. #include <Library/DebugLib.h>
  9. #include <Library/BaseMemoryLib.h>
  10. #include <Library/UefiBootServicesTableLib.h>
  11. #include <Library/UefiLib.h>
  12. #include <Library/PcdLib.h>
  13. #include <Library/IoLib.h>
  14. #include <Library/OmapLib.h>
  15. #include <Protocol/Timer.h>
  16. #include <Protocol/HardwareInterrupt.h>
  17. #include <Omap3530/Omap3530.h>
  18. // The notification function to call on every timer interrupt.
  19. volatile EFI_TIMER_NOTIFY mTimerNotifyFunction = (EFI_TIMER_NOTIFY)NULL;
  20. // The current period of the timer interrupt
  21. volatile UINT64 mTimerPeriod = 0;
  22. // Cached copy of the Hardware Interrupt protocol instance
  23. EFI_HARDWARE_INTERRUPT_PROTOCOL *gInterrupt = NULL;
  24. // Cached registers
  25. volatile UINT32 TISR;
  26. volatile UINT32 TCLR;
  27. volatile UINT32 TLDR;
  28. volatile UINT32 TCRR;
  29. volatile UINT32 TIER;
  30. // Cached interrupt vector
  31. volatile UINTN gVector;
  32. /**
  33. C Interrupt Handler calledin the interrupt context when Source interrupt is active.
  34. @param Source Source of the interrupt. Hardware routing off a specific platform defines
  35. what source means.
  36. @param SystemContext Pointer to system register context. Mostly used by debuggers and will
  37. update the system context after the return from the interrupt if
  38. modified. Don't change these values unless you know what you are doing
  39. **/
  40. VOID
  41. EFIAPI
  42. TimerInterruptHandler (
  43. IN HARDWARE_INTERRUPT_SOURCE Source,
  44. IN EFI_SYSTEM_CONTEXT SystemContext
  45. )
  46. {
  47. EFI_TPL OriginalTPL;
  48. //
  49. // DXE core uses this callback for the EFI timer tick. The DXE core uses locks
  50. // that raise to TPL_HIGH and then restore back to current level. Thus we need
  51. // to make sure TPL level is set to TPL_HIGH while we are handling the timer tick.
  52. //
  53. OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);
  54. if (mTimerNotifyFunction) {
  55. mTimerNotifyFunction(mTimerPeriod);
  56. }
  57. // Clear all timer interrupts
  58. MmioWrite32 (TISR, TISR_CLEAR_ALL);
  59. // Poll interrupt status bits to ensure clearing
  60. while ((MmioRead32 (TISR) & TISR_ALL_INTERRUPT_MASK) != TISR_NO_INTERRUPTS_PENDING);
  61. gBS->RestoreTPL (OriginalTPL);
  62. }
  63. /**
  64. This function registers the handler NotifyFunction so it is called every time
  65. the timer interrupt fires. It also passes the amount of time since the last
  66. handler call to the NotifyFunction. If NotifyFunction is NULL, then the
  67. handler is unregistered. If the handler is registered, then EFI_SUCCESS is
  68. returned. If the CPU does not support registering a timer interrupt handler,
  69. then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler
  70. when a handler is already registered, then EFI_ALREADY_STARTED is returned.
  71. If an attempt is made to unregister a handler when a handler is not registered,
  72. then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to
  73. register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR
  74. is returned.
  75. @param This The EFI_TIMER_ARCH_PROTOCOL instance.
  76. @param NotifyFunction The function to call when a timer interrupt fires. This
  77. function executes at TPL_HIGH_LEVEL. The DXE Core will
  78. register a handler for the timer interrupt, so it can know
  79. how much time has passed. This information is used to
  80. signal timer based events. NULL will unregister the handler.
  81. @retval EFI_SUCCESS The timer handler was registered.
  82. @retval EFI_UNSUPPORTED The platform does not support timer interrupts.
  83. @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already
  84. registered.
  85. @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not
  86. previously registered.
  87. @retval EFI_DEVICE_ERROR The timer handler could not be registered.
  88. **/
  89. EFI_STATUS
  90. EFIAPI
  91. TimerDriverRegisterHandler (
  92. IN EFI_TIMER_ARCH_PROTOCOL *This,
  93. IN EFI_TIMER_NOTIFY NotifyFunction
  94. )
  95. {
  96. if ((NotifyFunction == NULL) && (mTimerNotifyFunction == NULL)) {
  97. return EFI_INVALID_PARAMETER;
  98. }
  99. if ((NotifyFunction != NULL) && (mTimerNotifyFunction != NULL)) {
  100. return EFI_ALREADY_STARTED;
  101. }
  102. mTimerNotifyFunction = NotifyFunction;
  103. return EFI_SUCCESS;
  104. }
  105. /**
  106. This function adjusts the period of timer interrupts to the value specified
  107. by TimerPeriod. If the timer period is updated, then the selected timer
  108. period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
  109. the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
  110. If an error occurs while attempting to update the timer period, then the
  111. timer hardware will be put back in its state prior to this call, and
  112. EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
  113. is disabled. This is not the same as disabling the CPU's interrupts.
  114. Instead, it must either turn off the timer hardware, or it must adjust the
  115. interrupt controller so that a CPU interrupt is not generated when the timer
  116. interrupt fires.
  117. @param This The EFI_TIMER_ARCH_PROTOCOL instance.
  118. @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If
  119. the timer hardware is not programmable, then EFI_UNSUPPORTED is
  120. returned. If the timer is programmable, then the timer period
  121. will be rounded up to the nearest timer period that is supported
  122. by the timer hardware. If TimerPeriod is set to 0, then the
  123. timer interrupts will be disabled.
  124. @retval EFI_SUCCESS The timer period was changed.
  125. @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.
  126. @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.
  127. **/
  128. EFI_STATUS
  129. EFIAPI
  130. TimerDriverSetTimerPeriod (
  131. IN EFI_TIMER_ARCH_PROTOCOL *This,
  132. IN UINT64 TimerPeriod
  133. )
  134. {
  135. EFI_STATUS Status;
  136. UINT64 TimerCount;
  137. INT32 LoadValue;
  138. if (TimerPeriod == 0) {
  139. // Turn off GPTIMER3
  140. MmioWrite32 (TCLR, TCLR_ST_OFF);
  141. Status = gInterrupt->DisableInterruptSource(gInterrupt, gVector);
  142. } else {
  143. // Calculate required timer count
  144. TimerCount = DivU64x32(TimerPeriod * 100, PcdGet32(PcdEmbeddedPerformanceCounterPeriodInNanoseconds));
  145. // Set GPTIMER3 Load register
  146. LoadValue = (INT32) -TimerCount;
  147. MmioWrite32 (TLDR, LoadValue);
  148. MmioWrite32 (TCRR, LoadValue);
  149. // Enable Overflow interrupt
  150. MmioWrite32 (TIER, TIER_TCAR_IT_DISABLE | TIER_OVF_IT_ENABLE | TIER_MAT_IT_DISABLE);
  151. // Turn on GPTIMER3, it will reload at overflow
  152. MmioWrite32 (TCLR, TCLR_AR_AUTORELOAD | TCLR_ST_ON);
  153. Status = gInterrupt->EnableInterruptSource(gInterrupt, gVector);
  154. }
  155. //
  156. // Save the new timer period
  157. //
  158. mTimerPeriod = TimerPeriod;
  159. return Status;
  160. }
  161. /**
  162. This function retrieves the period of timer interrupts in 100 ns units,
  163. returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
  164. is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
  165. returned, then the timer is currently disabled.
  166. @param This The EFI_TIMER_ARCH_PROTOCOL instance.
  167. @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If
  168. 0 is returned, then the timer is currently disabled.
  169. @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
  170. @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
  171. **/
  172. EFI_STATUS
  173. EFIAPI
  174. TimerDriverGetTimerPeriod (
  175. IN EFI_TIMER_ARCH_PROTOCOL *This,
  176. OUT UINT64 *TimerPeriod
  177. )
  178. {
  179. if (TimerPeriod == NULL) {
  180. return EFI_INVALID_PARAMETER;
  181. }
  182. *TimerPeriod = mTimerPeriod;
  183. return EFI_SUCCESS;
  184. }
  185. /**
  186. This function generates a soft timer interrupt. If the platform does not support soft
  187. timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.
  188. If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()
  189. service, then a soft timer interrupt will be generated. If the timer interrupt is
  190. enabled when this service is called, then the registered handler will be invoked. The
  191. registered handler should not be able to distinguish a hardware-generated timer
  192. interrupt from a software-generated timer interrupt.
  193. @param This The EFI_TIMER_ARCH_PROTOCOL instance.
  194. @retval EFI_SUCCESS The soft timer interrupt was generated.
  195. @retval EFI_UNSUPPORTED The platform does not support the generation of soft timer interrupts.
  196. **/
  197. EFI_STATUS
  198. EFIAPI
  199. TimerDriverGenerateSoftInterrupt (
  200. IN EFI_TIMER_ARCH_PROTOCOL *This
  201. )
  202. {
  203. return EFI_UNSUPPORTED;
  204. }
  205. /**
  206. Interface stucture for the Timer Architectural Protocol.
  207. @par Protocol Description:
  208. This protocol provides the services to initialize a periodic timer
  209. interrupt, and to register a handler that is called each time the timer
  210. interrupt fires. It may also provide a service to adjust the rate of the
  211. periodic timer interrupt. When a timer interrupt occurs, the handler is
  212. passed the amount of time that has passed since the previous timer
  213. interrupt.
  214. @param RegisterHandler
  215. Registers a handler that will be called each time the
  216. timer interrupt fires. TimerPeriod defines the minimum
  217. time between timer interrupts, so TimerPeriod will also
  218. be the minimum time between calls to the registered
  219. handler.
  220. @param SetTimerPeriod
  221. Sets the period of the timer interrupt in 100 nS units.
  222. This function is optional, and may return EFI_UNSUPPORTED.
  223. If this function is supported, then the timer period will
  224. be rounded up to the nearest supported timer period.
  225. @param GetTimerPeriod
  226. Retrieves the period of the timer interrupt in 100 nS units.
  227. @param GenerateSoftInterrupt
  228. Generates a soft timer interrupt that simulates the firing of
  229. the timer interrupt. This service can be used to invoke the registered handler if the timer interrupt has been masked for
  230. a period of time.
  231. **/
  232. EFI_TIMER_ARCH_PROTOCOL gTimer = {
  233. TimerDriverRegisterHandler,
  234. TimerDriverSetTimerPeriod,
  235. TimerDriverGetTimerPeriod,
  236. TimerDriverGenerateSoftInterrupt
  237. };
  238. /**
  239. Initialize the state information for the Timer Architectural Protocol and
  240. the Timer Debug support protocol that allows the debugger to break into a
  241. running program.
  242. @param ImageHandle of the loaded driver
  243. @param SystemTable Pointer to the System Table
  244. @retval EFI_SUCCESS Protocol registered
  245. @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
  246. @retval EFI_DEVICE_ERROR Hardware problems
  247. **/
  248. EFI_STATUS
  249. EFIAPI
  250. TimerInitialize (
  251. IN EFI_HANDLE ImageHandle,
  252. IN EFI_SYSTEM_TABLE *SystemTable
  253. )
  254. {
  255. EFI_HANDLE Handle = NULL;
  256. EFI_STATUS Status;
  257. UINT32 TimerBaseAddress;
  258. // Find the interrupt controller protocol. ASSERT if not found.
  259. Status = gBS->LocateProtocol (&gHardwareInterruptProtocolGuid, NULL, (VOID **)&gInterrupt);
  260. ASSERT_EFI_ERROR (Status);
  261. // Set up the timer registers
  262. TimerBaseAddress = TimerBase (FixedPcdGet32(PcdOmap35xxArchTimer));
  263. TISR = TimerBaseAddress + GPTIMER_TISR;
  264. TCLR = TimerBaseAddress + GPTIMER_TCLR;
  265. TLDR = TimerBaseAddress + GPTIMER_TLDR;
  266. TCRR = TimerBaseAddress + GPTIMER_TCRR;
  267. TIER = TimerBaseAddress + GPTIMER_TIER;
  268. // Disable the timer
  269. Status = TimerDriverSetTimerPeriod (&gTimer, 0);
  270. ASSERT_EFI_ERROR (Status);
  271. // Install interrupt handler
  272. gVector = InterruptVectorForTimer (FixedPcdGet32(PcdOmap35xxArchTimer));
  273. Status = gInterrupt->RegisterInterruptSource (gInterrupt, gVector, TimerInterruptHandler);
  274. ASSERT_EFI_ERROR (Status);
  275. // Turn on the functional clock for Timer
  276. MmioOr32 (CM_FCLKEN_PER, CM_FCLKEN_PER_EN_GPT3_ENABLE);
  277. // Set up default timer
  278. Status = TimerDriverSetTimerPeriod (&gTimer, FixedPcdGet32(PcdTimerPeriod));
  279. ASSERT_EFI_ERROR (Status);
  280. // Install the Timer Architectural Protocol onto a new handle
  281. Status = gBS->InstallMultipleProtocolInterfaces (
  282. &Handle,
  283. &gEfiTimerArchProtocolGuid, &gTimer,
  284. NULL
  285. );
  286. ASSERT_EFI_ERROR(Status);
  287. return Status;
  288. }