DS3231RealTimeClockLib.c 13 KB

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