DebugAgentTimerLib.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /** @file
  2. Debug Agent timer lib for OMAP 35xx.
  3. Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Base.h>
  7. #include <Library/BaseLib.h>
  8. #include <Library/IoLib.h>
  9. #include <Library/OmapLib.h>
  10. #include <Library/ArmLib.h>
  11. #include <Library/PcdLib.h>
  12. #include <Omap3530/Omap3530.h>
  13. volatile UINT32 gVector;
  14. // Cached registers
  15. volatile UINT32 gTISR;
  16. volatile UINT32 gTCLR;
  17. volatile UINT32 gTLDR;
  18. volatile UINT32 gTCRR;
  19. volatile UINT32 gTIER;
  20. VOID
  21. EnableInterruptSource (
  22. VOID
  23. )
  24. {
  25. UINTN Bank;
  26. UINTN Bit;
  27. // Map vector to FIQ, IRQ is default
  28. MmioWrite32 (INTCPS_ILR (gVector), 1);
  29. Bank = gVector / 32;
  30. Bit = 1UL << (gVector % 32);
  31. MmioWrite32 (INTCPS_MIR_CLEAR(Bank), Bit);
  32. }
  33. VOID
  34. DisableInterruptSource (
  35. VOID
  36. )
  37. {
  38. UINTN Bank;
  39. UINTN Bit;
  40. Bank = gVector / 32;
  41. Bit = 1UL << (gVector % 32);
  42. MmioWrite32 (INTCPS_MIR_SET(Bank), Bit);
  43. }
  44. /**
  45. Setup all the hardware needed for the debug agents timer.
  46. This function is used to set up debug enviroment. It may enable interrupts.
  47. **/
  48. VOID
  49. EFIAPI
  50. DebugAgentTimerIntialize (
  51. VOID
  52. )
  53. {
  54. UINT32 TimerBaseAddress;
  55. UINT32 TimerNumber;
  56. TimerNumber = PcdGet32(PcdOmap35xxDebugAgentTimer);
  57. gVector = InterruptVectorForTimer (TimerNumber);
  58. // Set up the timer registers
  59. TimerBaseAddress = TimerBase (TimerNumber);
  60. gTISR = TimerBaseAddress + GPTIMER_TISR;
  61. gTCLR = TimerBaseAddress + GPTIMER_TCLR;
  62. gTLDR = TimerBaseAddress + GPTIMER_TLDR;
  63. gTCRR = TimerBaseAddress + GPTIMER_TCRR;
  64. gTIER = TimerBaseAddress + GPTIMER_TIER;
  65. if ((TimerNumber < 2) || (TimerNumber > 9)) {
  66. // This code assumes one the General Purpose timers is used
  67. // GPT2 - GPT9
  68. CpuDeadLoop ();
  69. }
  70. // Set source clock for GPT2 - GPT9 to SYS_CLK
  71. MmioOr32 (CM_CLKSEL_PER, 1 << (TimerNumber - 2));
  72. }
  73. /**
  74. Set the period for the debug agent timer. Zero means disable the timer.
  75. @param[in] TimerPeriodMilliseconds Frequency of the debug agent timer.
  76. **/
  77. VOID
  78. EFIAPI
  79. DebugAgentTimerSetPeriod (
  80. IN UINT32 TimerPeriodMilliseconds
  81. )
  82. {
  83. UINT64 TimerCount;
  84. INT32 LoadValue;
  85. if (TimerPeriodMilliseconds == 0) {
  86. // Turn off GPTIMER3
  87. MmioWrite32 (gTCLR, TCLR_ST_OFF);
  88. DisableInterruptSource ();
  89. } else {
  90. // Calculate required timer count
  91. TimerCount = DivU64x32(TimerPeriodMilliseconds * 1000000, PcdGet32(PcdDebugAgentTimerFreqNanoSeconds));
  92. // Set GPTIMER5 Load register
  93. LoadValue = (INT32) -TimerCount;
  94. MmioWrite32 (gTLDR, LoadValue);
  95. MmioWrite32 (gTCRR, LoadValue);
  96. // Enable Overflow interrupt
  97. MmioWrite32 (gTIER, TIER_TCAR_IT_DISABLE | TIER_OVF_IT_ENABLE | TIER_MAT_IT_DISABLE);
  98. // Turn on GPTIMER3, it will reload at overflow
  99. MmioWrite32 (gTCLR, TCLR_AR_AUTORELOAD | TCLR_ST_ON);
  100. EnableInterruptSource ();
  101. }
  102. }
  103. /**
  104. Perform End Of Interrupt for the debug agent timer. This is called in the
  105. interrupt handler after the interrupt has been processed.
  106. **/
  107. VOID
  108. EFIAPI
  109. DebugAgentTimerEndOfInterrupt (
  110. VOID
  111. )
  112. {
  113. // Clear all timer interrupts
  114. MmioWrite32 (gTISR, TISR_CLEAR_ALL);
  115. // Poll interrupt status bits to ensure clearing
  116. while ((MmioRead32 (gTISR) & TISR_ALL_INTERRUPT_MASK) != TISR_NO_INTERRUPTS_PENDING);
  117. MmioWrite32 (INTCPS_CONTROL, INTCPS_CONTROL_NEWFIQAGR);
  118. ArmDataSynchronizationBarrier ();
  119. }