DS3231RealTimeClockLib.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /** @file
  2. Implement EFI RealTimeClock runtime services via RTC Lib.
  3. Currently this driver does not support runtime virtual calling.
  4. Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
  5. Copyright (c) 2011-2013, ARM Ltd. All rights reserved.<BR>
  6. Copyright (c) 2015, Hisilicon Limited. All rights reserved.<BR>
  7. Copyright (c) 2015, Linaro Limited. All rights reserved.<BR>
  8. SPDX-License-Identifier: BSD-2-Clause-Patent
  9. Based on the files under ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.inf
  10. **/
  11. #include <Uefi.h>
  12. #include <PiDxe.h>
  13. #include <Library/BaseLib.h>
  14. #include <Library/BaseMemoryLib.h>
  15. #include <Library/DebugLib.h>
  16. #include <Library/UefiLib.h>
  17. // Use EfiAtRuntime to check stage
  18. #include <Library/UefiRuntimeLib.h>
  19. #include <Library/IoLib.h>
  20. #include <Library/MemoryAllocationLib.h>
  21. #include <Library/PcdLib.h>
  22. #include <Library/UefiBootServicesTableLib.h>
  23. #include <Library/UefiRuntimeServicesTableLib.h>
  24. #include <Library/TimerLib.h>
  25. #include <Library/TimeBaseLib.h>
  26. #include <Protocol/RealTimeClock.h>
  27. #include <Library/I2CLib.h>
  28. #include "DS3231RealTimeClock.h"
  29. #include <Library/CpldD03.h>
  30. #include <Library/CpldIoLib.h>
  31. extern I2C_DEVICE gRtcDevice;
  32. STATIC BOOLEAN mDS3231Initialized = FALSE;
  33. EFI_STATUS
  34. IdentifyDS3231 (
  35. VOID
  36. )
  37. {
  38. EFI_STATUS Status;
  39. Status = EFI_SUCCESS;
  40. return Status;
  41. }
  42. EFI_STATUS
  43. SwitchRtcI2cChannelAndLock (
  44. VOID
  45. )
  46. {
  47. UINT8 Temp;
  48. UINT8 Count;
  49. for (Count = 0; Count < 20; Count++) {
  50. Temp = ReadCpldReg (CPLD_I2C_SWITCH_FLAG);
  51. if ((Temp & BMC_I2C_STATUS) != 0) {
  52. //The I2C channel is shared with BMC,
  53. //Check if BMC has taken ownership of I2C.
  54. //If so, wait 30ms, then try again.
  55. //If not, start using I2C.
  56. //And the CPLD_I2C_SWITCH_FLAG will be set to CPU_GET_I2C_CONTROL
  57. //BMC will check this flag to decide to use I2C or not.
  58. MicroSecondDelay (30000);
  59. continue;
  60. }
  61. Temp = ReadCpldReg (CPLD_I2C_SWITCH_FLAG);
  62. Temp = Temp | CPU_GET_I2C_CONTROL;
  63. WriteCpldReg (CPLD_I2C_SWITCH_FLAG, Temp);
  64. //This is empirical value,give cpld some time to make sure the
  65. //value is wrote in
  66. MicroSecondDelay (2);
  67. Temp = ReadCpldReg (CPLD_I2C_SWITCH_FLAG);
  68. if ((Temp & CPU_GET_I2C_CONTROL) == CPU_GET_I2C_CONTROL) {
  69. return EFI_SUCCESS;
  70. }
  71. //There need 30ms to keep consistent with the previous loops if the CPU failed
  72. //to get control of I2C
  73. MicroSecondDelay (30000);
  74. }
  75. Temp = ReadCpldReg (CPLD_I2C_SWITCH_FLAG);
  76. Temp = Temp & ~CPU_GET_I2C_CONTROL;
  77. WriteCpldReg (CPLD_I2C_SWITCH_FLAG, Temp);
  78. return EFI_NOT_READY;
  79. }
  80. EFI_STATUS
  81. InitializeDS3231 (
  82. VOID
  83. )
  84. {
  85. EFI_STATUS Status;
  86. I2C_DEVICE Dev;
  87. RTC_DS3231_CONTROL Temp;
  88. RTC_DS3231_HOURS Hours;
  89. // Prepare the hardware
  90. (VOID)IdentifyDS3231();
  91. (VOID) CopyMem (&Dev, &gRtcDevice, sizeof(Dev));
  92. Status = I2CInit(Dev.Socket,Dev.Port,Normal);
  93. if (EFI_ERROR (Status)) {
  94. goto EXIT;
  95. }
  96. // Ensure interrupts are masked. We do not want RTC interrupts in UEFI
  97. Status = I2CRead(&Dev,DS3231_REGADDR_CONTROL,1,&Temp.u8);
  98. if (EFI_ERROR (Status)) {
  99. goto EXIT;
  100. }
  101. Temp.bits.INTCN = 0;
  102. Status = I2CWrite(&Dev,DS3231_REGADDR_CONTROL,1,&Temp.u8);
  103. if (EFI_ERROR (Status)) {
  104. goto EXIT;
  105. }
  106. MicroSecondDelay(2000);
  107. Status = I2CRead(&Dev,DS3231_REGADDR_HOURS,1,&Hours.u8);
  108. if (EFI_ERROR (Status)) {
  109. goto EXIT;
  110. }
  111. Hours.bits.Hour24_n = 0;
  112. Status = I2CWrite(&Dev,DS3231_REGADDR_HOURS,1,&Hours.u8);
  113. if (EFI_ERROR (Status)) {
  114. goto EXIT;
  115. }
  116. mDS3231Initialized = TRUE;
  117. EXIT:
  118. return Status;
  119. }
  120. /**
  121. Returns the current time and date information, and the time-keeping capabilities
  122. of the hardware platform.
  123. @param Time A pointer to storage to receive a snapshot of the current time.
  124. @param Capabilities An optional pointer to a buffer to receive the real time clock
  125. device's capabilities.
  126. @retval EFI_SUCCESS The operation completed successfully.
  127. @retval EFI_INVALID_PARAMETER Time is NULL.
  128. @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
  129. @retval EFI_SECURITY_VIOLATION The time could not be retrieved due to an authentication failure.
  130. **/
  131. EFI_STATUS
  132. EFIAPI
  133. LibGetTime (
  134. OUT EFI_TIME *Time,
  135. OUT EFI_TIME_CAPABILITIES *Capabilities
  136. )
  137. {
  138. EFI_STATUS Status = EFI_SUCCESS;
  139. UINT8 Temp;
  140. UINT8 BaseHour = 0;
  141. UINT16 BaseYear = 1900;
  142. I2C_DEVICE Dev;
  143. // Ensure Time is a valid pointer
  144. if (NULL == Time) {
  145. return EFI_INVALID_PARAMETER;
  146. }
  147. Status = SwitchRtcI2cChannelAndLock();
  148. if(EFI_ERROR (Status)) {
  149. return Status;
  150. }
  151. // Initialize the hardware if not already done
  152. if (!mDS3231Initialized) {
  153. Status = InitializeDS3231 ();
  154. if (EFI_ERROR (Status)) {
  155. Status = EFI_NOT_READY;
  156. goto GExit;
  157. }
  158. }
  159. (VOID) CopyMem (&Dev, &gRtcDevice, sizeof(Dev));
  160. Status |= I2CRead(&Dev,DS3231_REGADDR_MONTH,1,&Temp);
  161. Time->Month = ((Temp>>4)&1)*10+(Temp&0x0F);
  162. if(Temp&0x80){
  163. BaseYear = 2000;
  164. }
  165. Status |= I2CRead(&Dev,DS3231_REGADDR_YEAR,1,&Temp);
  166. Time->Year = BaseYear+(Temp>>4) *10 + (Temp&0x0F);
  167. Status |= I2CRead(&Dev,DS3231_REGADDR_DATE,1,&Temp);
  168. Time->Day = ((Temp>>4)&3) *10 + (Temp&0x0F);
  169. Status |= I2CRead(&Dev,DS3231_REGADDR_HOURS,1,&Temp);
  170. BaseHour = 0;
  171. if((Temp&0x30) == 0x30){
  172. Status = EFI_DEVICE_ERROR;
  173. goto GExit;
  174. }else if(Temp&0x20){
  175. BaseHour = 20;
  176. }else if(Temp&0x10){
  177. BaseHour = 10;
  178. }
  179. Time->Hour = BaseHour + (Temp&0x0F);
  180. Status |= I2CRead(&Dev,DS3231_REGADDR_MIUTES,1,&Temp);
  181. Time->Minute = ((Temp>>4)&7) * 10 + (Temp&0x0F);
  182. Status |= I2CRead(&Dev,DS3231_REGADDR_SECONDS,1,&Temp);
  183. Time->Second = (Temp>>4) * 10 + (Temp&0x0F);
  184. Time->Nanosecond = 0;
  185. Time->Daylight = 0;
  186. Time->TimeZone = EFI_UNSPECIFIED_TIMEZONE;
  187. if((EFI_ERROR(Status)) || (!IsTimeValid(Time)) || ((Time->Year - BaseYear) > 99)) {
  188. Status = EFI_UNSUPPORTED;
  189. }
  190. GExit:
  191. Temp = ReadCpldReg (CPLD_I2C_SWITCH_FLAG);
  192. Temp = Temp & ~CPU_GET_I2C_CONTROL;
  193. WriteCpldReg (CPLD_I2C_SWITCH_FLAG, Temp);
  194. return Status;
  195. }
  196. /**
  197. Sets the current local time and date information.
  198. @param Time A pointer to the current time.
  199. @retval EFI_SUCCESS The operation completed successfully.
  200. @retval EFI_INVALID_PARAMETER A time field is out of range.
  201. @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
  202. **/
  203. EFI_STATUS
  204. EFIAPI
  205. LibSetTime (
  206. IN EFI_TIME *Time
  207. )
  208. {
  209. EFI_STATUS Status = EFI_SUCCESS;
  210. I2C_DEVICE Dev;
  211. UINT8 Temp;
  212. UINT16 BaseYear = 1900;
  213. // Check the input parameters are within the range specified by UEFI
  214. if(!IsTimeValid(Time)){
  215. return EFI_INVALID_PARAMETER;
  216. }
  217. Status = SwitchRtcI2cChannelAndLock();
  218. if(EFI_ERROR (Status)) {
  219. return Status;
  220. }
  221. // Initialize the hardware if not already done
  222. if (!mDS3231Initialized) {
  223. Status = InitializeDS3231 ();
  224. if (EFI_ERROR (Status)) {
  225. goto EXIT;
  226. }
  227. }
  228. (VOID) CopyMem (&Dev, &gRtcDevice, sizeof(Dev));
  229. Temp = ((Time->Second/10)<<4) | (Time->Second%10);
  230. MicroSecondDelay(1000);
  231. Status = I2CWrite(&Dev,DS3231_REGADDR_SECONDS,1,&Temp);
  232. if(EFI_ERROR (Status)){
  233. goto EXIT;
  234. }
  235. Temp = ((Time->Minute/10)<<4) | (Time->Minute%10);
  236. MicroSecondDelay(1000);
  237. Status = I2CWrite(&Dev,DS3231_REGADDR_MIUTES,1,&Temp);
  238. if(EFI_ERROR (Status)){
  239. goto EXIT;
  240. }
  241. Temp = 0;
  242. if(Time->Hour > 19){
  243. Temp = 2;
  244. } else if(Time->Hour > 9){
  245. Temp = 1;
  246. }
  247. Temp = (Temp << 4) | (Time->Hour%10);
  248. MicroSecondDelay(1000);
  249. Status = I2CWrite(&Dev,DS3231_REGADDR_HOURS,1,&Temp);
  250. if(EFI_ERROR (Status)){
  251. goto EXIT;
  252. }
  253. Temp = ((Time->Day/10)<<4) | (Time->Day%10);
  254. MicroSecondDelay(1000);
  255. Status = I2CWrite(&Dev,DS3231_REGADDR_DATE,1,&Temp);
  256. if(EFI_ERROR (Status)){
  257. goto EXIT;
  258. }
  259. Temp = 0;
  260. if(Time->Year >= 2000){
  261. Temp = 0x8;
  262. BaseYear = 2000;
  263. }
  264. if(Time->Month > 9){
  265. Temp |= 0x1;
  266. }
  267. Temp = (Temp<<4) | (Time->Month%10);
  268. MicroSecondDelay(1000);
  269. Status = I2CWrite(&Dev,DS3231_REGADDR_MONTH,1,&Temp);
  270. if(EFI_ERROR (Status)){
  271. goto EXIT;
  272. }
  273. Temp = (((Time->Year-BaseYear)/10)<<4) | (Time->Year%10);
  274. MicroSecondDelay(1000);
  275. Status = I2CWrite(&Dev,DS3231_REGADDR_YEAR,1,&Temp);
  276. if(EFI_ERROR (Status)){
  277. goto EXIT;
  278. }
  279. EXIT:
  280. Temp = ReadCpldReg (CPLD_I2C_SWITCH_FLAG);
  281. Temp = Temp & ~CPU_GET_I2C_CONTROL;
  282. WriteCpldReg (CPLD_I2C_SWITCH_FLAG, Temp);
  283. return Status;
  284. }
  285. /**
  286. Returns the current wakeup alarm clock setting.
  287. @param Enabled Indicates if the alarm is currently enabled or disabled.
  288. @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
  289. @param Time The current alarm setting.
  290. @retval EFI_SUCCESS The alarm settings were returned.
  291. @retval EFI_INVALID_PARAMETER Any parameter is NULL.
  292. @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
  293. **/
  294. EFI_STATUS
  295. EFIAPI
  296. LibGetWakeupTime (
  297. OUT BOOLEAN *Enabled,
  298. OUT BOOLEAN *Pending,
  299. OUT EFI_TIME *Time
  300. )
  301. {
  302. // Not a required feature
  303. return EFI_UNSUPPORTED;
  304. }
  305. /**
  306. Sets the system wakeup alarm clock time.
  307. @param Enabled Enable or disable the wakeup alarm.
  308. @param Time If Enable is TRUE, the time to set the wakeup alarm for.
  309. @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
  310. Enable is FALSE, then the wakeup alarm was disabled.
  311. @retval EFI_INVALID_PARAMETER A time field is out of range.
  312. @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
  313. @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
  314. **/
  315. EFI_STATUS
  316. EFIAPI
  317. LibSetWakeupTime (
  318. IN BOOLEAN Enabled,
  319. OUT EFI_TIME *Time
  320. )
  321. {
  322. // Not a required feature
  323. return EFI_UNSUPPORTED;
  324. }
  325. /**
  326. This is the declaration of an EFI image entry point. This can be the entry point to an application
  327. written to this specification, an EFI boot service driver, or an EFI runtime driver.
  328. @param ImageHandle Handle that identifies the loaded image.
  329. @param SystemTable System Table for this image.
  330. @retval EFI_SUCCESS The operation completed successfully.
  331. **/
  332. EFI_STATUS
  333. EFIAPI
  334. LibRtcInitialize (
  335. IN EFI_HANDLE ImageHandle,
  336. IN EFI_SYSTEM_TABLE *SystemTable
  337. )
  338. {
  339. EFI_STATUS Status;
  340. EFI_HANDLE Handle;
  341. EFI_TIME EfiTime;
  342. // Setup the setters and getters
  343. gRT->GetTime = LibGetTime;
  344. gRT->SetTime = LibSetTime;
  345. gRT->GetWakeupTime = LibGetWakeupTime;
  346. gRT->SetWakeupTime = LibSetWakeupTime;
  347. (VOID)gRT->GetTime (&EfiTime, NULL);
  348. if((EfiTime.Year < 2015) || (EfiTime.Year > 2099)){
  349. EfiTime.Year = 2015;
  350. EfiTime.Month = 1;
  351. EfiTime.Day = 1;
  352. EfiTime.Hour = 0;
  353. EfiTime.Minute = 0;
  354. EfiTime.Second = 0;
  355. EfiTime.Nanosecond = 0;
  356. Status = gRT->SetTime(&EfiTime);
  357. if (EFI_ERROR(Status))
  358. {
  359. DEBUG((EFI_D_ERROR, "[%a]:[%dL] Status : %r\n", __FUNCTION__, __LINE__, Status));
  360. }
  361. }
  362. // Install the protocol
  363. Handle = NULL;
  364. Status = gBS->InstallMultipleProtocolInterfaces (
  365. &Handle,
  366. &gEfiRealTimeClockArchProtocolGuid, NULL,
  367. NULL
  368. );
  369. return Status;
  370. }
  371. /**
  372. Fixup internal data so that EFI can be call in virtual mode.
  373. Call the passed in Child Notify event and convert any pointers in
  374. lib to virtual mode.
  375. @param[in] Event The Event that is being processed
  376. @param[in] Context Event Context
  377. **/
  378. VOID
  379. EFIAPI
  380. LibRtcVirtualNotifyEvent (
  381. IN EFI_EVENT Event,
  382. IN VOID *Context
  383. )
  384. {
  385. //
  386. // Only needed if you are going to support the OS calling RTC functions in virtual mode.
  387. // You will need to call EfiConvertPointer (). To convert any stored physical addresses
  388. // to virtual address. After the OS transitions to calling in virtual mode, all future
  389. // runtime calls will be made in virtual mode.
  390. //
  391. return;
  392. }