]> git.proxmox.com Git - mirror_edk2.git/blame - Omap35xxPkg/Library/RealTimeClockLib/RealTimeClockLib.c
ARM Packages: Removed trailing spaces
[mirror_edk2.git] / Omap35xxPkg / Library / RealTimeClockLib / RealTimeClockLib.c
CommitLineData
1e57a462 1/** @file\r
2*\r
3* Copyright (c) 2011, ARM Limited. All rights reserved.\r
1e57a462 4*\r
3402aac7
RC
5* This program and the accompanying materials\r
6* are licensed and made available under the terms and conditions of the BSD License\r
7* which accompanies this distribution. The full text of the license may be found at\r
8* http://opensource.org/licenses/bsd-license.php\r
9*\r
10* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
1e57a462 12*\r
13**/\r
14\r
15#include <Uefi.h>\r
16\r
17#include <Library/BaseMemoryLib.h>\r
18#include <Library/UefiBootServicesTableLib.h>\r
19#include <Library/UefiRuntimeServicesTableLib.h>\r
20#include <Library/DebugLib.h>\r
21#include <Library/IoLib.h>\r
22\r
23#include <Protocol/RealTimeClock.h>\r
24#include <Protocol/EmbeddedExternalDevice.h>\r
25\r
26#include <Omap3530/Omap3530.h>\r
27#include <TPS65950.h>\r
28\r
29\r
30EMBEDDED_EXTERNAL_DEVICE *gTPS65950;\r
31INT16 TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
32\r
33/**\r
34 Returns the current time and date information, and the time-keeping capabilities\r
35 of the hardware platform.\r
36\r
37 @param Time A pointer to storage to receive a snapshot of the current time.\r
38 @param Capabilities An optional pointer to a buffer to receive the real time clock\r
39 device's capabilities.\r
40\r
41 @retval EFI_SUCCESS The operation completed successfully.\r
42 @retval EFI_INVALID_PARAMETER Time is NULL.\r
43 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.\r
44\r
45**/\r
46EFI_STATUS\r
47EFIAPI\r
48LibGetTime (\r
49 OUT EFI_TIME *Time,\r
50 OUT EFI_TIME_CAPABILITIES *Capabilities\r
51 )\r
52{\r
53 EFI_STATUS Status;\r
54 UINT8 Data;\r
55 EFI_TPL OldTpl;\r
56\r
57 if (Time == NULL) {\r
58 return EFI_INVALID_PARAMETER;\r
59 }\r
60\r
61 OldTpl = gBS->RaiseTPL(TPL_NOTIFY);\r
62\r
63 /* Get time and date */\r
64 ZeroMem(Time, sizeof(EFI_TIME));\r
65\r
66 // Latch values\r
67 Status = gTPS65950->Read (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, RTC_CTRL_REG), 1, &Data);\r
68 if (Status != EFI_SUCCESS) goto EXIT;\r
69 Data |= BIT6;\r
70 Status = gTPS65950->Write (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, RTC_CTRL_REG), 1, &Data);\r
71 if (Status != EFI_SUCCESS) goto EXIT;\r
72\r
73 // Read registers\r
74 Status = gTPS65950->Read (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, YEARS_REG), 1, &Data);\r
75 if (Status != EFI_SUCCESS) goto EXIT;\r
76 Time->Year = 2000 + ((Data >> 4) & 0xF) * 10 + (Data & 0xF);\r
77\r
78 Status = gTPS65950->Read (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, MONTHS_REG), 1, &Data);\r
79 if (Status != EFI_SUCCESS) goto EXIT;\r
80 Time->Month = ((Data >> 4) & 0x1) * 10 + (Data & 0xF);\r
81\r
82 Status = gTPS65950->Read (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, DAYS_REG), 1, &Data);\r
83 if (Status != EFI_SUCCESS) goto EXIT;\r
84 Time->Day = ((Data >> 4) & 0x3) * 10 + (Data & 0xF);\r
85\r
86 Status = gTPS65950->Read (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, HOURS_REG), 1, &Data);\r
87 if (Status != EFI_SUCCESS) goto EXIT;\r
88 Time->Hour = ((Data >> 4) & 0x3) * 10 + (Data & 0xF);\r
89\r
90 Status = gTPS65950->Read (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, MINUTES_REG), 1, &Data);\r
91 if (Status != EFI_SUCCESS) goto EXIT;\r
92 Time->Minute = ((Data >> 4) & 0x7) * 10 + (Data & 0xF);\r
93\r
94 Status = gTPS65950->Read (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, SECONDS_REG), 1, &Data);\r
95 if (Status != EFI_SUCCESS) goto EXIT;\r
96 Time->Second = ((Data >> 4) & 0x7) * 10 + (Data & 0xF);\r
97\r
98 Time->TimeZone = TimeZone;\r
99 // TODO: check what to use here\r
100 Time->Daylight = EFI_TIME_ADJUST_DAYLIGHT;\r
101\r
102 // Set capabilities\r
103\r
104 // TODO: Set real capabilities\r
105 if (Capabilities != NULL) {\r
106 Capabilities->Resolution = 1;\r
107 Capabilities->Accuracy = 50000000;\r
108 Capabilities->SetsToZero = FALSE;\r
109 }\r
110\r
111EXIT:\r
112 gBS->RestoreTPL(OldTpl);\r
113\r
114 return (Status == EFI_SUCCESS) ? Status : EFI_DEVICE_ERROR;\r
115}\r
116\r
117/**\r
118 Sets the current local time and date information.\r
119\r
120 @param Time A pointer to the current time.\r
121\r
122 @retval EFI_SUCCESS The operation completed successfully.\r
123 @retval EFI_INVALID_PARAMETER A time field is out of range.\r
124 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.\r
125\r
126**/\r
127EFI_STATUS\r
128EFIAPI\r
129LibSetTime (\r
130 IN EFI_TIME *Time\r
131 )\r
132{\r
133 EFI_STATUS Status;\r
134 UINT8 Data;\r
135 UINT8 MonthDayCount[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };\r
136 EFI_TPL OldTpl;\r
137\r
138 // Input validation according both to UEFI spec and hardware constraints\r
139 // UEFI spec says valid year range is 1900-9999 but TPS only supports 2000-2099\r
140 if ( (Time == NULL)\r
141 || (Time->Year < 2000 || Time->Year > 2099)\r
142 || (Time->Month < 1 || Time->Month > 12)\r
143 || (Time->Day < 1 || Time->Day > MonthDayCount[Time->Month])\r
144 || (Time->Hour > 23)\r
145 || (Time->Minute > 59)\r
146 || (Time->Second > 59)\r
147 || (Time->Nanosecond > 999999999)\r
148 || ((Time->TimeZone < -1440 || Time->TimeZone > 1440) && Time->TimeZone != 2047)\r
149 ) {\r
150 return EFI_INVALID_PARAMETER;\r
151 }\r
152\r
153 OldTpl = gBS->RaiseTPL(TPL_NOTIFY);\r
154\r
155 Data = Time->Year - 2000;\r
156 Status = gTPS65950->Write (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, YEARS_REG), 1, &Data);\r
157 if (Status != EFI_SUCCESS) goto EXIT;\r
158\r
159 Data = ((Time->Month / 10) << 4) | (Time->Month % 10);\r
160 Status = gTPS65950->Write (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, MONTHS_REG), 1, &Data);\r
161 if (Status != EFI_SUCCESS) goto EXIT;\r
162\r
163 Data = ((Time->Day / 10) << 4) | (Time->Day % 10);\r
164 Status = gTPS65950->Write (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, DAYS_REG), 1, &Data);\r
165 if (Status != EFI_SUCCESS) goto EXIT;\r
166\r
167 Data = ((Time->Hour / 10) << 4) | (Time->Hour % 10);\r
168 Status = gTPS65950->Write (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, HOURS_REG), 1, &Data);\r
169 if (Status != EFI_SUCCESS) goto EXIT;\r
170\r
171 Data = ((Time->Minute / 10) << 4) | (Time->Minute % 10);\r
172 Status = gTPS65950->Write (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, MINUTES_REG), 1, &Data);\r
173 if (Status != EFI_SUCCESS) goto EXIT;\r
174\r
175 Data = ((Time->Second / 10) << 4) | (Time->Second % 10);\r
176 Status = gTPS65950->Write (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, SECONDS_REG), 1, &Data);\r
177 if (Status != EFI_SUCCESS) goto EXIT;\r
178\r
179 TimeZone = Time->TimeZone;\r
180\r
181EXIT:\r
182 gBS->RestoreTPL(OldTpl);\r
183\r
184 return (Status == EFI_SUCCESS) ? Status : EFI_DEVICE_ERROR;\r
185}\r
186\r
187/**\r
188 Returns the current wakeup alarm clock setting.\r
189\r
190 @param Enabled Indicates if the alarm is currently enabled or disabled.\r
191 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.\r
192 @param Time The current alarm setting.\r
193\r
194 @retval EFI_SUCCESS The alarm settings were returned.\r
195 @retval EFI_INVALID_PARAMETER Any parameter is NULL.\r
196 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.\r
197\r
198**/\r
199EFI_STATUS\r
200EFIAPI\r
201LibGetWakeupTime (\r
202 OUT BOOLEAN *Enabled,\r
203 OUT BOOLEAN *Pending,\r
204 OUT EFI_TIME *Time\r
205 )\r
206{\r
207 return EFI_UNSUPPORTED;\r
208}\r
209\r
210/**\r
211 Sets the system wakeup alarm clock time.\r
212\r
213 @param Enabled Enable or disable the wakeup alarm.\r
214 @param Time If Enable is TRUE, the time to set the wakeup alarm for.\r
215\r
216 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If\r
217 Enable is FALSE, then the wakeup alarm was disabled.\r
218 @retval EFI_INVALID_PARAMETER A time field is out of range.\r
219 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.\r
220 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.\r
221\r
222**/\r
223EFI_STATUS\r
224EFIAPI\r
225LibSetWakeupTime (\r
226 IN BOOLEAN Enabled,\r
227 OUT EFI_TIME *Time\r
228 )\r
229{\r
230 return EFI_UNSUPPORTED;\r
231}\r
232\r
233/**\r
234 This is the declaration of an EFI image entry point. This can be the entry point to an application\r
235 written to this specification, an EFI boot service driver, or an EFI runtime driver.\r
236\r
237 @param ImageHandle Handle that identifies the loaded image.\r
238 @param SystemTable System Table for this image.\r
239\r
240 @retval EFI_SUCCESS The operation completed successfully.\r
241\r
242**/\r
243EFI_STATUS\r
244EFIAPI\r
245LibRtcInitialize (\r
246 IN EFI_HANDLE ImageHandle,\r
247 IN EFI_SYSTEM_TABLE *SystemTable\r
248 )\r
249{\r
250 EFI_STATUS Status;\r
251 EFI_HANDLE Handle;\r
252 UINT8 Data;\r
253 EFI_TPL OldTpl;\r
254\r
255 Status = gBS->LocateProtocol (&gEmbeddedExternalDeviceProtocolGuid, NULL, (VOID **)&gTPS65950);\r
256 ASSERT_EFI_ERROR(Status);\r
257\r
258 OldTpl = gBS->RaiseTPL(TPL_NOTIFY);\r
259 Data = 1;\r
260 Status = gTPS65950->Write (gTPS65950, EXTERNAL_DEVICE_REGISTER(I2C_ADDR_GRP_ID4, RTC_CTRL_REG), 1, &Data);\r
261 ASSERT_EFI_ERROR(Status);\r
262 gBS->RestoreTPL(OldTpl);\r
263\r
264 // Setup the setters and getters\r
265 gRT->GetTime = LibGetTime;\r
266 gRT->SetTime = LibSetTime;\r
267 gRT->GetWakeupTime = LibGetWakeupTime;\r
268 gRT->SetWakeupTime = LibSetWakeupTime;\r
269\r
270 // Install the protocol\r
271 Handle = NULL;\r
272 Status = gBS->InstallMultipleProtocolInterfaces (\r
273 &Handle,\r
274 &gEfiRealTimeClockArchProtocolGuid, NULL,\r
275 NULL\r
276 );\r
277\r
278 return Status;\r
279}\r
280\r
281/**\r
282 Fixup internal data so that EFI can be call in virtual mode.\r
283 Call the passed in Child Notify event and convert any pointers in\r
284 lib to virtual mode.\r
285\r
286 @param[in] Event The Event that is being processed\r
287 @param[in] Context Event Context\r
288**/\r
289VOID\r
290EFIAPI\r
291LibRtcVirtualNotifyEvent (\r
292 IN EFI_EVENT Event,\r
293 IN VOID *Context\r
294 )\r
295{\r
296 return;\r
297}\r