3 * Copyright (c) 2011, ARM Limited. All rights reserved.
5 * SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Library/BaseMemoryLib.h>
12 #include <Library/UefiBootServicesTableLib.h>
13 #include <Library/UefiRuntimeServicesTableLib.h>
14 #include <Library/DebugLib.h>
15 #include <Library/IoLib.h>
17 #include <Protocol/RealTimeClock.h>
18 #include <Protocol/EmbeddedExternalDevice.h>
20 #include <Omap3530/Omap3530.h>
24 EMBEDDED_EXTERNAL_DEVICE
*gTPS65950
;
25 INT16 TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
28 Returns the current time and date information, and the time-keeping capabilities
29 of the hardware platform.
31 @param Time A pointer to storage to receive a snapshot of the current time.
32 @param Capabilities An optional pointer to a buffer to receive the real time clock
33 device's capabilities.
35 @retval EFI_SUCCESS The operation completed successfully.
36 @retval EFI_INVALID_PARAMETER Time is NULL.
37 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
44 OUT EFI_TIME_CAPABILITIES
*Capabilities
52 return EFI_INVALID_PARAMETER
;
55 OldTpl
= gBS
->RaiseTPL(TPL_NOTIFY
);
57 /* Get time and date */
58 ZeroMem(Time
, sizeof(EFI_TIME
));
61 Status
= gTPS65950
->Read (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4
, RTC_CTRL_REG
), 1, &Data
);
62 if (Status
!= EFI_SUCCESS
) goto EXIT
;
64 Status
= gTPS65950
->Write (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4
, RTC_CTRL_REG
), 1, &Data
);
65 if (Status
!= EFI_SUCCESS
) goto EXIT
;
68 Status
= gTPS65950
->Read (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4
, YEARS_REG
), 1, &Data
);
69 if (Status
!= EFI_SUCCESS
) goto EXIT
;
70 Time
->Year
= 2000 + ((Data
>> 4) & 0xF) * 10 + (Data
& 0xF);
72 Status
= gTPS65950
->Read (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4
, MONTHS_REG
), 1, &Data
);
73 if (Status
!= EFI_SUCCESS
) goto EXIT
;
74 Time
->Month
= ((Data
>> 4) & 0x1) * 10 + (Data
& 0xF);
76 Status
= gTPS65950
->Read (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4
, DAYS_REG
), 1, &Data
);
77 if (Status
!= EFI_SUCCESS
) goto EXIT
;
78 Time
->Day
= ((Data
>> 4) & 0x3) * 10 + (Data
& 0xF);
80 Status
= gTPS65950
->Read (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4
, HOURS_REG
), 1, &Data
);
81 if (Status
!= EFI_SUCCESS
) goto EXIT
;
82 Time
->Hour
= ((Data
>> 4) & 0x3) * 10 + (Data
& 0xF);
84 Status
= gTPS65950
->Read (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4
, MINUTES_REG
), 1, &Data
);
85 if (Status
!= EFI_SUCCESS
) goto EXIT
;
86 Time
->Minute
= ((Data
>> 4) & 0x7) * 10 + (Data
& 0xF);
88 Status
= gTPS65950
->Read (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4
, SECONDS_REG
), 1, &Data
);
89 if (Status
!= EFI_SUCCESS
) goto EXIT
;
90 Time
->Second
= ((Data
>> 4) & 0x7) * 10 + (Data
& 0xF);
92 Time
->TimeZone
= TimeZone
;
93 // TODO: check what to use here
94 Time
->Daylight
= EFI_TIME_ADJUST_DAYLIGHT
;
98 // TODO: Set real capabilities
99 if (Capabilities
!= NULL
) {
100 Capabilities
->Resolution
= 1;
101 Capabilities
->Accuracy
= 50000000;
102 Capabilities
->SetsToZero
= FALSE
;
106 gBS
->RestoreTPL(OldTpl
);
108 return (Status
== EFI_SUCCESS
) ? Status
: EFI_DEVICE_ERROR
;
112 Sets the current local time and date information.
114 @param Time A pointer to the current time.
116 @retval EFI_SUCCESS The operation completed successfully.
117 @retval EFI_INVALID_PARAMETER A time field is out of range.
118 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
129 UINT8 MonthDayCount
[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
132 // Input validation according both to UEFI spec and hardware constraints
133 // UEFI spec says valid year range is 1900-9999 but TPS only supports 2000-2099
135 || (Time
->Year
< 2000 || Time
->Year
> 2099)
136 || (Time
->Month
< 1 || Time
->Month
> 12)
137 || (Time
->Day
< 1 || Time
->Day
> MonthDayCount
[Time
->Month
])
139 || (Time
->Minute
> 59)
140 || (Time
->Second
> 59)
141 || (Time
->Nanosecond
> 999999999)
142 || ((Time
->TimeZone
< -1440 || Time
->TimeZone
> 1440) && Time
->TimeZone
!= 2047)
144 return EFI_INVALID_PARAMETER
;
147 OldTpl
= gBS
->RaiseTPL(TPL_NOTIFY
);
149 Data
= Time
->Year
- 2000;
150 Status
= gTPS65950
->Write (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4
, YEARS_REG
), 1, &Data
);
151 if (Status
!= EFI_SUCCESS
) goto EXIT
;
153 Data
= ((Time
->Month
/ 10) << 4) | (Time
->Month
% 10);
154 Status
= gTPS65950
->Write (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4
, MONTHS_REG
), 1, &Data
);
155 if (Status
!= EFI_SUCCESS
) goto EXIT
;
157 Data
= ((Time
->Day
/ 10) << 4) | (Time
->Day
% 10);
158 Status
= gTPS65950
->Write (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4
, DAYS_REG
), 1, &Data
);
159 if (Status
!= EFI_SUCCESS
) goto EXIT
;
161 Data
= ((Time
->Hour
/ 10) << 4) | (Time
->Hour
% 10);
162 Status
= gTPS65950
->Write (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4
, HOURS_REG
), 1, &Data
);
163 if (Status
!= EFI_SUCCESS
) goto EXIT
;
165 Data
= ((Time
->Minute
/ 10) << 4) | (Time
->Minute
% 10);
166 Status
= gTPS65950
->Write (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4
, MINUTES_REG
), 1, &Data
);
167 if (Status
!= EFI_SUCCESS
) goto EXIT
;
169 Data
= ((Time
->Second
/ 10) << 4) | (Time
->Second
% 10);
170 Status
= gTPS65950
->Write (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4
, SECONDS_REG
), 1, &Data
);
171 if (Status
!= EFI_SUCCESS
) goto EXIT
;
173 TimeZone
= Time
->TimeZone
;
176 gBS
->RestoreTPL(OldTpl
);
178 return (Status
== EFI_SUCCESS
) ? Status
: EFI_DEVICE_ERROR
;
182 Returns the current wakeup alarm clock setting.
184 @param Enabled Indicates if the alarm is currently enabled or disabled.
185 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
186 @param Time The current alarm setting.
188 @retval EFI_SUCCESS The alarm settings were returned.
189 @retval EFI_INVALID_PARAMETER Any parameter is NULL.
190 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
196 OUT BOOLEAN
*Enabled
,
197 OUT BOOLEAN
*Pending
,
201 return EFI_UNSUPPORTED
;
205 Sets the system wakeup alarm clock time.
207 @param Enabled Enable or disable the wakeup alarm.
208 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
210 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
211 Enable is FALSE, then the wakeup alarm was disabled.
212 @retval EFI_INVALID_PARAMETER A time field is out of range.
213 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
214 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
224 return EFI_UNSUPPORTED
;
228 This is the declaration of an EFI image entry point. This can be the entry point to an application
229 written to this specification, an EFI boot service driver, or an EFI runtime driver.
231 @param ImageHandle Handle that identifies the loaded image.
232 @param SystemTable System Table for this image.
234 @retval EFI_SUCCESS The operation completed successfully.
240 IN EFI_HANDLE ImageHandle
,
241 IN EFI_SYSTEM_TABLE
*SystemTable
249 Status
= gBS
->LocateProtocol (&gEmbeddedExternalDeviceProtocolGuid
, NULL
, (VOID
**)&gTPS65950
);
250 ASSERT_EFI_ERROR(Status
);
252 OldTpl
= gBS
->RaiseTPL(TPL_NOTIFY
);
254 Status
= gTPS65950
->Write (gTPS65950
, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4
, RTC_CTRL_REG
), 1, &Data
);
255 ASSERT_EFI_ERROR(Status
);
256 gBS
->RestoreTPL(OldTpl
);
258 // Setup the setters and getters
259 gRT
->GetTime
= LibGetTime
;
260 gRT
->SetTime
= LibSetTime
;
261 gRT
->GetWakeupTime
= LibGetWakeupTime
;
262 gRT
->SetWakeupTime
= LibSetWakeupTime
;
264 // Install the protocol
266 Status
= gBS
->InstallMultipleProtocolInterfaces (
268 &gEfiRealTimeClockArchProtocolGuid
, NULL
,
276 Fixup internal data so that EFI can be call in virtual mode.
277 Call the passed in Child Notify event and convert any pointers in
280 @param[in] Event The Event that is being processed
281 @param[in] Context Event Context
285 LibRtcVirtualNotifyEvent (