]> git.proxmox.com Git - mirror_edk2.git/blob - Omap35xxPkg/Library/RealTimeClockLib/RealTimeClockLib.c
ddde1868ac0112d9dd827ca40590785116a55604
[mirror_edk2.git] / Omap35xxPkg / Library / RealTimeClockLib / RealTimeClockLib.c
1 /** @file
2 *
3 * Copyright (c) 2011, ARM Limited. All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-2-Clause-Patent
6 *
7 **/
8
9 #include <Uefi.h>
10
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>
16
17 #include <Protocol/RealTimeClock.h>
18 #include <Protocol/EmbeddedExternalDevice.h>
19
20 #include <Omap3530/Omap3530.h>
21 #include <TPS65950.h>
22
23
24 EMBEDDED_EXTERNAL_DEVICE *gTPS65950;
25 INT16 TimeZone = EFI_UNSPECIFIED_TIMEZONE;
26
27 /**
28 Returns the current time and date information, and the time-keeping capabilities
29 of the hardware platform.
30
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.
34
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.
38
39 **/
40 EFI_STATUS
41 EFIAPI
42 LibGetTime (
43 OUT EFI_TIME *Time,
44 OUT EFI_TIME_CAPABILITIES *Capabilities
45 )
46 {
47 EFI_STATUS Status;
48 UINT8 Data;
49 EFI_TPL OldTpl;
50
51 if (Time == NULL) {
52 return EFI_INVALID_PARAMETER;
53 }
54
55 OldTpl = gBS->RaiseTPL(TPL_NOTIFY);
56
57 /* Get time and date */
58 ZeroMem(Time, sizeof(EFI_TIME));
59
60 // Latch values
61 Status = gTPS65950->Read (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, RTC_CTRL_REG), 1, &Data);
62 if (Status != EFI_SUCCESS) goto EXIT;
63 Data |= BIT6;
64 Status = gTPS65950->Write (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, RTC_CTRL_REG), 1, &Data);
65 if (Status != EFI_SUCCESS) goto EXIT;
66
67 // Read registers
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);
71
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);
75
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);
79
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);
83
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);
87
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);
91
92 Time->TimeZone = TimeZone;
93 // TODO: check what to use here
94 Time->Daylight = EFI_TIME_ADJUST_DAYLIGHT;
95
96 // Set capabilities
97
98 // TODO: Set real capabilities
99 if (Capabilities != NULL) {
100 Capabilities->Resolution = 1;
101 Capabilities->Accuracy = 50000000;
102 Capabilities->SetsToZero = FALSE;
103 }
104
105 EXIT:
106 gBS->RestoreTPL(OldTpl);
107
108 return (Status == EFI_SUCCESS) ? Status : EFI_DEVICE_ERROR;
109 }
110
111 /**
112 Sets the current local time and date information.
113
114 @param Time A pointer to the current time.
115
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.
119
120 **/
121 EFI_STATUS
122 EFIAPI
123 LibSetTime (
124 IN EFI_TIME *Time
125 )
126 {
127 EFI_STATUS Status;
128 UINT8 Data;
129 UINT8 MonthDayCount[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
130 EFI_TPL OldTpl;
131
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
134 if ( (Time == NULL)
135 || (Time->Year < 2000 || Time->Year > 2099)
136 || (Time->Month < 1 || Time->Month > 12)
137 || (Time->Day < 1 || Time->Day > MonthDayCount[Time->Month])
138 || (Time->Hour > 23)
139 || (Time->Minute > 59)
140 || (Time->Second > 59)
141 || (Time->Nanosecond > 999999999)
142 || ((Time->TimeZone < -1440 || Time->TimeZone > 1440) && Time->TimeZone != 2047)
143 ) {
144 return EFI_INVALID_PARAMETER;
145 }
146
147 OldTpl = gBS->RaiseTPL(TPL_NOTIFY);
148
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;
152
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;
156
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;
160
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;
164
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;
168
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;
172
173 TimeZone = Time->TimeZone;
174
175 EXIT:
176 gBS->RestoreTPL(OldTpl);
177
178 return (Status == EFI_SUCCESS) ? Status : EFI_DEVICE_ERROR;
179 }
180
181 /**
182 Returns the current wakeup alarm clock setting.
183
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.
187
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.
191
192 **/
193 EFI_STATUS
194 EFIAPI
195 LibGetWakeupTime (
196 OUT BOOLEAN *Enabled,
197 OUT BOOLEAN *Pending,
198 OUT EFI_TIME *Time
199 )
200 {
201 return EFI_UNSUPPORTED;
202 }
203
204 /**
205 Sets the system wakeup alarm clock time.
206
207 @param Enabled Enable or disable the wakeup alarm.
208 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
209
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.
215
216 **/
217 EFI_STATUS
218 EFIAPI
219 LibSetWakeupTime (
220 IN BOOLEAN Enabled,
221 OUT EFI_TIME *Time
222 )
223 {
224 return EFI_UNSUPPORTED;
225 }
226
227 /**
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.
230
231 @param ImageHandle Handle that identifies the loaded image.
232 @param SystemTable System Table for this image.
233
234 @retval EFI_SUCCESS The operation completed successfully.
235
236 **/
237 EFI_STATUS
238 EFIAPI
239 LibRtcInitialize (
240 IN EFI_HANDLE ImageHandle,
241 IN EFI_SYSTEM_TABLE *SystemTable
242 )
243 {
244 EFI_STATUS Status;
245 EFI_HANDLE Handle;
246 UINT8 Data;
247 EFI_TPL OldTpl;
248
249 Status = gBS->LocateProtocol (&gEmbeddedExternalDeviceProtocolGuid, NULL, (VOID **)&gTPS65950);
250 ASSERT_EFI_ERROR(Status);
251
252 OldTpl = gBS->RaiseTPL(TPL_NOTIFY);
253 Data = 1;
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);
257
258 // Setup the setters and getters
259 gRT->GetTime = LibGetTime;
260 gRT->SetTime = LibSetTime;
261 gRT->GetWakeupTime = LibGetWakeupTime;
262 gRT->SetWakeupTime = LibSetWakeupTime;
263
264 // Install the protocol
265 Handle = NULL;
266 Status = gBS->InstallMultipleProtocolInterfaces (
267 &Handle,
268 &gEfiRealTimeClockArchProtocolGuid, NULL,
269 NULL
270 );
271
272 return Status;
273 }
274
275 /**
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
278 lib to virtual mode.
279
280 @param[in] Event The Event that is being processed
281 @param[in] Context Event Context
282 **/
283 VOID
284 EFIAPI
285 LibRtcVirtualNotifyEvent (
286 IN EFI_EVENT Event,
287 IN VOID *Context
288 )
289 {
290 return;
291 }