KvmtoolRtcFdtClientLib.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /** @file
  2. FDT client library for motorola,mc146818 RTC driver
  3. Copyright (c) 2020, ARM Limited. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Library/BaseLib.h>
  7. #include <Library/DebugLib.h>
  8. #include <Library/DxeServicesTableLib.h>
  9. #include <Library/PcdLib.h>
  10. #include <Library/UefiBootServicesTableLib.h>
  11. #include <Protocol/FdtClient.h>
  12. /** RTC Index register is at offset 0x0
  13. */
  14. #define RTC_INDEX_REG_OFFSET 0x0ULL
  15. /** RTC Target register is at offset 0x1
  16. */
  17. #define RTC_TARGET_REG_OFFSET 0x1ULL
  18. /** Add the RTC controller address range to the memory map.
  19. @param [in] ImageHandle The handle to the image.
  20. @param [in] RtcPageBase Base address of the RTC controller.
  21. @retval EFI_SUCCESS Success.
  22. @retval EFI_INVALID_PARAMETER A parameter is invalid.
  23. @retval EFI_NOT_FOUND Flash device not found.
  24. **/
  25. STATIC
  26. EFI_STATUS
  27. KvmtoolRtcMapMemory (
  28. IN EFI_HANDLE ImageHandle,
  29. IN EFI_PHYSICAL_ADDRESS RtcPageBase
  30. )
  31. {
  32. EFI_STATUS Status;
  33. Status = gDS->AddMemorySpace (
  34. EfiGcdMemoryTypeMemoryMappedIo,
  35. RtcPageBase,
  36. EFI_PAGE_SIZE,
  37. EFI_MEMORY_UC | EFI_MEMORY_RUNTIME
  38. );
  39. if (EFI_ERROR (Status)) {
  40. DEBUG ((
  41. DEBUG_ERROR,
  42. "Failed to add memory space. Status = %r\n",
  43. Status
  44. ));
  45. return Status;
  46. }
  47. Status = gDS->AllocateMemorySpace (
  48. EfiGcdAllocateAddress,
  49. EfiGcdMemoryTypeMemoryMappedIo,
  50. 0,
  51. EFI_PAGE_SIZE,
  52. &RtcPageBase,
  53. ImageHandle,
  54. NULL
  55. );
  56. if (EFI_ERROR (Status)) {
  57. DEBUG ((
  58. DEBUG_ERROR,
  59. "Failed to allocate memory space. Status = %r\n",
  60. Status
  61. ));
  62. gDS->RemoveMemorySpace (
  63. RtcPageBase,
  64. EFI_PAGE_SIZE
  65. );
  66. return Status;
  67. }
  68. Status = gDS->SetMemorySpaceAttributes (
  69. RtcPageBase,
  70. EFI_PAGE_SIZE,
  71. EFI_MEMORY_UC | EFI_MEMORY_RUNTIME
  72. );
  73. if (EFI_ERROR (Status)) {
  74. DEBUG ((
  75. DEBUG_ERROR,
  76. "Failed to set memory attributes. Status = %r\n",
  77. Status
  78. ));
  79. gDS->FreeMemorySpace (
  80. RtcPageBase,
  81. EFI_PAGE_SIZE
  82. );
  83. gDS->RemoveMemorySpace (
  84. RtcPageBase,
  85. EFI_PAGE_SIZE
  86. );
  87. }
  88. return Status;
  89. }
  90. /** Entrypoint for KvmtoolRtcFdtClientLib.
  91. Locate the RTC node in the DT and update the Index and
  92. Target register base addresses in the respective PCDs.
  93. Add the RTC memory region to the memory map.
  94. Disable the RTC node as the RTC is owned by UEFI.
  95. @param [in] ImageHandle The handle to the image.
  96. @param [in] SystemTable Pointer to the System Table.
  97. @retval EFI_SUCCESS Success.
  98. @retval EFI_INVALID_PARAMETER A parameter is invalid.
  99. @retval EFI_NOT_FOUND Flash device not found.
  100. **/
  101. EFI_STATUS
  102. EFIAPI
  103. KvmtoolRtcFdtClientLibConstructor (
  104. IN EFI_HANDLE ImageHandle,
  105. IN EFI_SYSTEM_TABLE *SystemTable
  106. )
  107. {
  108. EFI_STATUS Status;
  109. FDT_CLIENT_PROTOCOL *FdtClient;
  110. INT32 Node;
  111. CONST UINT32 *Reg;
  112. UINT32 RegSize;
  113. UINT64 RegBase;
  114. UINT64 Range;
  115. RETURN_STATUS PcdStatus;
  116. Status = gBS->LocateProtocol (
  117. &gFdtClientProtocolGuid,
  118. NULL,
  119. (VOID **)&FdtClient
  120. );
  121. ASSERT_EFI_ERROR (Status);
  122. Status = FdtClient->FindCompatibleNode (
  123. FdtClient,
  124. "motorola,mc146818",
  125. &Node
  126. );
  127. if (EFI_ERROR (Status)) {
  128. DEBUG ((
  129. DEBUG_ERROR,
  130. "%a: No 'motorola,mc146818' compatible DT node found\n",
  131. __FUNCTION__
  132. ));
  133. return Status;
  134. }
  135. Status = FdtClient->GetNodeProperty (
  136. FdtClient,
  137. Node,
  138. "reg",
  139. (CONST VOID **)&Reg,
  140. &RegSize
  141. );
  142. if (EFI_ERROR (Status)) {
  143. DEBUG ((
  144. DEBUG_ERROR,
  145. "%a: No 'reg' property found in 'motorola,mc146818' compatible DT node\n",
  146. __FUNCTION__
  147. ));
  148. return Status;
  149. }
  150. ASSERT (RegSize == 16);
  151. RegBase = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[0]));
  152. Range = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[2]));
  153. DEBUG ((
  154. DEBUG_INFO,
  155. "Found motorola,mc146818 RTC @ 0x%Lx Range = 0x%x\n",
  156. RegBase,
  157. Range
  158. ));
  159. // The address range must cover the RTC Index and the Target registers.
  160. ASSERT (Range >= 0x2);
  161. // RTC Index register is at offset 0x0
  162. PcdStatus = PcdSet64S (
  163. PcdRtcIndexRegister64,
  164. (RegBase + RTC_INDEX_REG_OFFSET)
  165. );
  166. ASSERT_RETURN_ERROR (PcdStatus);
  167. // RTC Target register is at offset 0x1
  168. PcdStatus = PcdSet64S (
  169. PcdRtcTargetRegister64,
  170. (RegBase + RTC_TARGET_REG_OFFSET)
  171. );
  172. ASSERT_RETURN_ERROR (PcdStatus);
  173. Status = KvmtoolRtcMapMemory (ImageHandle, (RegBase & ~EFI_PAGE_MASK));
  174. if (EFI_ERROR (Status)) {
  175. DEBUG ((
  176. DEBUG_ERROR,
  177. "Failed to map memory for motorola,mc146818. Status = %r\n",
  178. Status
  179. ));
  180. return Status;
  181. }
  182. //
  183. // UEFI takes ownership of the RTC hardware, and exposes its functionality
  184. // through the UEFI Runtime Services GetTime, SetTime, etc. This means we
  185. // need to disable it in the device tree to prevent the OS from attaching
  186. // its device driver as well.
  187. //
  188. Status = FdtClient->SetNodeProperty (
  189. FdtClient,
  190. Node,
  191. "status",
  192. "disabled",
  193. sizeof ("disabled")
  194. );
  195. if (EFI_ERROR (Status)) {
  196. DEBUG ((
  197. DEBUG_WARN,
  198. "Failed to set motorola,mc146818 status to 'disabled', Status = %r\n",
  199. Status
  200. ));
  201. }
  202. return EFI_SUCCESS;
  203. }