RealTimeClockLib.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /** @file
  2. *
  3. * Copyright (c) 2011, ARM Limited. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause-Patent
  6. *
  7. **/
  8. #include <Uefi.h>
  9. #include <Library/BaseMemoryLib.h>
  10. #include <Library/UefiBootServicesTableLib.h>
  11. #include <Library/UefiRuntimeServicesTableLib.h>
  12. #include <Library/DebugLib.h>
  13. #include <Library/IoLib.h>
  14. #include <Protocol/RealTimeClock.h>
  15. #include <Protocol/EmbeddedExternalDevice.h>
  16. #include <Omap3530/Omap3530.h>
  17. #include <TPS65950.h>
  18. EMBEDDED_EXTERNAL_DEVICE *gTPS65950;
  19. INT16 TimeZone = EFI_UNSPECIFIED_TIMEZONE;
  20. /**
  21. Returns the current time and date information, and the time-keeping capabilities
  22. of the hardware platform.
  23. @param Time A pointer to storage to receive a snapshot of the current time.
  24. @param Capabilities An optional pointer to a buffer to receive the real time clock
  25. device's capabilities.
  26. @retval EFI_SUCCESS The operation completed successfully.
  27. @retval EFI_INVALID_PARAMETER Time is NULL.
  28. @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
  29. **/
  30. EFI_STATUS
  31. EFIAPI
  32. LibGetTime (
  33. OUT EFI_TIME *Time,
  34. OUT EFI_TIME_CAPABILITIES *Capabilities
  35. )
  36. {
  37. EFI_STATUS Status;
  38. UINT8 Data;
  39. EFI_TPL OldTpl;
  40. if (Time == NULL) {
  41. return EFI_INVALID_PARAMETER;
  42. }
  43. OldTpl = gBS->RaiseTPL(TPL_NOTIFY);
  44. /* Get time and date */
  45. ZeroMem(Time, sizeof(EFI_TIME));
  46. // Latch values
  47. Status = gTPS65950->Read (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, RTC_CTRL_REG), 1, &Data);
  48. if (Status != EFI_SUCCESS) goto EXIT;
  49. Data |= BIT6;
  50. Status = gTPS65950->Write (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, RTC_CTRL_REG), 1, &Data);
  51. if (Status != EFI_SUCCESS) goto EXIT;
  52. // Read registers
  53. Status = gTPS65950->Read (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, YEARS_REG), 1, &Data);
  54. if (Status != EFI_SUCCESS) goto EXIT;
  55. Time->Year = 2000 + ((Data >> 4) & 0xF) * 10 + (Data & 0xF);
  56. Status = gTPS65950->Read (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, MONTHS_REG), 1, &Data);
  57. if (Status != EFI_SUCCESS) goto EXIT;
  58. Time->Month = ((Data >> 4) & 0x1) * 10 + (Data & 0xF);
  59. Status = gTPS65950->Read (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, DAYS_REG), 1, &Data);
  60. if (Status != EFI_SUCCESS) goto EXIT;
  61. Time->Day = ((Data >> 4) & 0x3) * 10 + (Data & 0xF);
  62. Status = gTPS65950->Read (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, HOURS_REG), 1, &Data);
  63. if (Status != EFI_SUCCESS) goto EXIT;
  64. Time->Hour = ((Data >> 4) & 0x3) * 10 + (Data & 0xF);
  65. Status = gTPS65950->Read (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, MINUTES_REG), 1, &Data);
  66. if (Status != EFI_SUCCESS) goto EXIT;
  67. Time->Minute = ((Data >> 4) & 0x7) * 10 + (Data & 0xF);
  68. Status = gTPS65950->Read (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, SECONDS_REG), 1, &Data);
  69. if (Status != EFI_SUCCESS) goto EXIT;
  70. Time->Second = ((Data >> 4) & 0x7) * 10 + (Data & 0xF);
  71. Time->TimeZone = TimeZone;
  72. // TODO: check what to use here
  73. Time->Daylight = EFI_TIME_ADJUST_DAYLIGHT;
  74. // Set capabilities
  75. // TODO: Set real capabilities
  76. if (Capabilities != NULL) {
  77. Capabilities->Resolution = 1;
  78. Capabilities->Accuracy = 50000000;
  79. Capabilities->SetsToZero = FALSE;
  80. }
  81. EXIT:
  82. gBS->RestoreTPL(OldTpl);
  83. return (Status == EFI_SUCCESS) ? Status : EFI_DEVICE_ERROR;
  84. }
  85. /**
  86. Sets the current local time and date information.
  87. @param Time A pointer to the current time.
  88. @retval EFI_SUCCESS The operation completed successfully.
  89. @retval EFI_INVALID_PARAMETER A time field is out of range.
  90. @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
  91. **/
  92. EFI_STATUS
  93. EFIAPI
  94. LibSetTime (
  95. IN EFI_TIME *Time
  96. )
  97. {
  98. EFI_STATUS Status;
  99. UINT8 Data;
  100. UINT8 MonthDayCount[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  101. EFI_TPL OldTpl;
  102. // Input validation according both to UEFI spec and hardware constraints
  103. // UEFI spec says valid year range is 1900-9999 but TPS only supports 2000-2099
  104. if ( (Time == NULL)
  105. || (Time->Year < 2000 || Time->Year > 2099)
  106. || (Time->Month < 1 || Time->Month > 12)
  107. || (Time->Day < 1 || Time->Day > MonthDayCount[Time->Month])
  108. || (Time->Hour > 23)
  109. || (Time->Minute > 59)
  110. || (Time->Second > 59)
  111. || (Time->Nanosecond > 999999999)
  112. || ((Time->TimeZone < -1440 || Time->TimeZone > 1440) && Time->TimeZone != 2047)
  113. ) {
  114. return EFI_INVALID_PARAMETER;
  115. }
  116. OldTpl = gBS->RaiseTPL(TPL_NOTIFY);
  117. Data = Time->Year - 2000;
  118. Status = gTPS65950->Write (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, YEARS_REG), 1, &Data);
  119. if (Status != EFI_SUCCESS) goto EXIT;
  120. Data = ((Time->Month / 10) << 4) | (Time->Month % 10);
  121. Status = gTPS65950->Write (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, MONTHS_REG), 1, &Data);
  122. if (Status != EFI_SUCCESS) goto EXIT;
  123. Data = ((Time->Day / 10) << 4) | (Time->Day % 10);
  124. Status = gTPS65950->Write (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, DAYS_REG), 1, &Data);
  125. if (Status != EFI_SUCCESS) goto EXIT;
  126. Data = ((Time->Hour / 10) << 4) | (Time->Hour % 10);
  127. Status = gTPS65950->Write (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, HOURS_REG), 1, &Data);
  128. if (Status != EFI_SUCCESS) goto EXIT;
  129. Data = ((Time->Minute / 10) << 4) | (Time->Minute % 10);
  130. Status = gTPS65950->Write (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, MINUTES_REG), 1, &Data);
  131. if (Status != EFI_SUCCESS) goto EXIT;
  132. Data = ((Time->Second / 10) << 4) | (Time->Second % 10);
  133. Status = gTPS65950->Write (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, SECONDS_REG), 1, &Data);
  134. if (Status != EFI_SUCCESS) goto EXIT;
  135. TimeZone = Time->TimeZone;
  136. EXIT:
  137. gBS->RestoreTPL(OldTpl);
  138. return (Status == EFI_SUCCESS) ? Status : EFI_DEVICE_ERROR;
  139. }
  140. /**
  141. Returns the current wakeup alarm clock setting.
  142. @param Enabled Indicates if the alarm is currently enabled or disabled.
  143. @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
  144. @param Time The current alarm setting.
  145. @retval EFI_SUCCESS The alarm settings were returned.
  146. @retval EFI_INVALID_PARAMETER Any parameter is NULL.
  147. @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
  148. **/
  149. EFI_STATUS
  150. EFIAPI
  151. LibGetWakeupTime (
  152. OUT BOOLEAN *Enabled,
  153. OUT BOOLEAN *Pending,
  154. OUT EFI_TIME *Time
  155. )
  156. {
  157. return EFI_UNSUPPORTED;
  158. }
  159. /**
  160. Sets the system wakeup alarm clock time.
  161. @param Enabled Enable or disable the wakeup alarm.
  162. @param Time If Enable is TRUE, the time to set the wakeup alarm for.
  163. @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
  164. Enable is FALSE, then the wakeup alarm was disabled.
  165. @retval EFI_INVALID_PARAMETER A time field is out of range.
  166. @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
  167. @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
  168. **/
  169. EFI_STATUS
  170. EFIAPI
  171. LibSetWakeupTime (
  172. IN BOOLEAN Enabled,
  173. OUT EFI_TIME *Time
  174. )
  175. {
  176. return EFI_UNSUPPORTED;
  177. }
  178. /**
  179. This is the declaration of an EFI image entry point. This can be the entry point to an application
  180. written to this specification, an EFI boot service driver, or an EFI runtime driver.
  181. @param ImageHandle Handle that identifies the loaded image.
  182. @param SystemTable System Table for this image.
  183. @retval EFI_SUCCESS The operation completed successfully.
  184. **/
  185. EFI_STATUS
  186. EFIAPI
  187. LibRtcInitialize (
  188. IN EFI_HANDLE ImageHandle,
  189. IN EFI_SYSTEM_TABLE *SystemTable
  190. )
  191. {
  192. EFI_STATUS Status;
  193. EFI_HANDLE Handle;
  194. UINT8 Data;
  195. EFI_TPL OldTpl;
  196. Status = gBS->LocateProtocol (&gEmbeddedExternalDeviceProtocolGuid, NULL, (VOID **)&gTPS65950);
  197. ASSERT_EFI_ERROR(Status);
  198. OldTpl = gBS->RaiseTPL(TPL_NOTIFY);
  199. Data = 1;
  200. Status = gTPS65950->Write (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, RTC_CTRL_REG), 1, &Data);
  201. ASSERT_EFI_ERROR(Status);
  202. gBS->RestoreTPL(OldTpl);
  203. // Setup the setters and getters
  204. gRT->GetTime = LibGetTime;
  205. gRT->SetTime = LibSetTime;
  206. gRT->GetWakeupTime = LibGetWakeupTime;
  207. gRT->SetWakeupTime = LibSetWakeupTime;
  208. // Install the protocol
  209. Handle = NULL;
  210. Status = gBS->InstallMultipleProtocolInterfaces (
  211. &Handle,
  212. &gEfiRealTimeClockArchProtocolGuid, NULL,
  213. NULL
  214. );
  215. return Status;
  216. }
  217. /**
  218. Fixup internal data so that EFI can be call in virtual mode.
  219. Call the passed in Child Notify event and convert any pointers in
  220. lib to virtual mode.
  221. @param[in] Event The Event that is being processed
  222. @param[in] Context Event Context
  223. **/
  224. VOID
  225. EFIAPI
  226. LibRtcVirtualNotifyEvent (
  227. IN EFI_EVENT Event,
  228. IN VOID *Context
  229. )
  230. {
  231. return;
  232. }