1 | /** @file
|
---|
2 | Header file for real time clock driver.
|
---|
3 |
|
---|
4 | Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
|
---|
5 | Copyright (c) 2017, AMD Inc. All rights reserved.<BR>
|
---|
6 |
|
---|
7 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
8 |
|
---|
9 | **/
|
---|
10 |
|
---|
11 | #ifndef _RTC_H_
|
---|
12 | #define _RTC_H_
|
---|
13 |
|
---|
14 | #include <Uefi.h>
|
---|
15 |
|
---|
16 | #include <Guid/Acpi.h>
|
---|
17 |
|
---|
18 | #include <Protocol/RealTimeClock.h>
|
---|
19 |
|
---|
20 | #include <Library/BaseLib.h>
|
---|
21 | #include <Library/DebugLib.h>
|
---|
22 | #include <Library/UefiLib.h>
|
---|
23 | #include <Library/BaseMemoryLib.h>
|
---|
24 | #include <Library/IoLib.h>
|
---|
25 | #include <Library/TimerLib.h>
|
---|
26 | #include <Library/UefiDriverEntryPoint.h>
|
---|
27 | #include <Library/UefiBootServicesTableLib.h>
|
---|
28 | #include <Library/UefiRuntimeLib.h>
|
---|
29 | #include <Library/UefiRuntimeServicesTableLib.h>
|
---|
30 | #include <Library/PcdLib.h>
|
---|
31 | #include <Library/ReportStatusCodeLib.h>
|
---|
32 |
|
---|
33 | typedef struct {
|
---|
34 | EFI_LOCK RtcLock;
|
---|
35 | INT16 SavedTimeZone;
|
---|
36 | UINT8 Daylight;
|
---|
37 | UINT8 CenturyRtcAddress;
|
---|
38 | } PC_RTC_MODULE_GLOBALS;
|
---|
39 |
|
---|
40 | extern PC_RTC_MODULE_GLOBALS mModuleGlobal;
|
---|
41 |
|
---|
42 | //
|
---|
43 | // Dallas DS12C887 Real Time Clock
|
---|
44 | //
|
---|
45 | #define RTC_ADDRESS_SECONDS 0 // R/W Range 0..59
|
---|
46 | #define RTC_ADDRESS_SECONDS_ALARM 1 // R/W Range 0..59
|
---|
47 | #define RTC_ADDRESS_MINUTES 2 // R/W Range 0..59
|
---|
48 | #define RTC_ADDRESS_MINUTES_ALARM 3 // R/W Range 0..59
|
---|
49 | #define RTC_ADDRESS_HOURS 4 // R/W Range 1..12 or 0..23 Bit 7 is AM/PM
|
---|
50 | #define RTC_ADDRESS_HOURS_ALARM 5 // R/W Range 1..12 or 0..23 Bit 7 is AM/PM
|
---|
51 | #define RTC_ADDRESS_DAY_OF_THE_WEEK 6 // R/W Range 1..7
|
---|
52 | #define RTC_ADDRESS_DAY_OF_THE_MONTH 7 // R/W Range 1..31
|
---|
53 | #define RTC_ADDRESS_MONTH 8 // R/W Range 1..12
|
---|
54 | #define RTC_ADDRESS_YEAR 9 // R/W Range 0..99
|
---|
55 | #define RTC_ADDRESS_REGISTER_A 10 // R/W[0..6] R0[7]
|
---|
56 | #define RTC_ADDRESS_REGISTER_B 11 // R/W
|
---|
57 | #define RTC_ADDRESS_REGISTER_C 12 // RO
|
---|
58 | #define RTC_ADDRESS_REGISTER_D 13 // RO
|
---|
59 | //
|
---|
60 | // Date and time initial values.
|
---|
61 | // They are used if the RTC values are invalid during driver initialization
|
---|
62 | //
|
---|
63 | #define RTC_INIT_SECOND 0
|
---|
64 | #define RTC_INIT_MINUTE 0
|
---|
65 | #define RTC_INIT_HOUR 0
|
---|
66 | #define RTC_INIT_DAY 1
|
---|
67 | #define RTC_INIT_MONTH 1
|
---|
68 |
|
---|
69 | #pragma pack(1)
|
---|
70 | //
|
---|
71 | // Register A
|
---|
72 | //
|
---|
73 | #define RTC_DIV_RESET 0x70
|
---|
74 | typedef struct {
|
---|
75 | UINT8 Rs : 4; // Rate Selection Bits
|
---|
76 | UINT8 Dv : 3; // Divisor
|
---|
77 | UINT8 Uip : 1; // Update in progress
|
---|
78 | } RTC_REGISTER_A_BITS;
|
---|
79 |
|
---|
80 | typedef union {
|
---|
81 | RTC_REGISTER_A_BITS Bits;
|
---|
82 | UINT8 Data;
|
---|
83 | } RTC_REGISTER_A;
|
---|
84 |
|
---|
85 | //
|
---|
86 | // Register B
|
---|
87 | //
|
---|
88 | typedef struct {
|
---|
89 | UINT8 Dse : 1; // 0 - Daylight saving disabled 1 - Daylight savings enabled
|
---|
90 | UINT8 Mil : 1; // 0 - 12 hour mode 1 - 24 hour mode
|
---|
91 | UINT8 Dm : 1; // 0 - BCD Format 1 - Binary Format
|
---|
92 | UINT8 Sqwe : 1; // 0 - Disable SQWE output 1 - Enable SQWE output
|
---|
93 | UINT8 Uie : 1; // 0 - Update INT disabled 1 - Update INT enabled
|
---|
94 | UINT8 Aie : 1; // 0 - Alarm INT disabled 1 - Alarm INT Enabled
|
---|
95 | UINT8 Pie : 1; // 0 - Periodic INT disabled 1 - Periodic INT Enabled
|
---|
96 | UINT8 Set : 1; // 0 - Normal operation. 1 - Updates inhibited
|
---|
97 | } RTC_REGISTER_B_BITS;
|
---|
98 |
|
---|
99 | typedef union {
|
---|
100 | RTC_REGISTER_B_BITS Bits;
|
---|
101 | UINT8 Data;
|
---|
102 | } RTC_REGISTER_B;
|
---|
103 |
|
---|
104 | //
|
---|
105 | // Register C
|
---|
106 | //
|
---|
107 | typedef struct {
|
---|
108 | UINT8 Reserved : 4; // Read as zero. Can not be written.
|
---|
109 | UINT8 Uf : 1; // Update End Interrupt Flag
|
---|
110 | UINT8 Af : 1; // Alarm Interrupt Flag
|
---|
111 | UINT8 Pf : 1; // Periodic Interrupt Flag
|
---|
112 | UINT8 Irqf : 1; // Interrupt Request Flag = PF & PIE | AF & AIE | UF & UIE
|
---|
113 | } RTC_REGISTER_C_BITS;
|
---|
114 |
|
---|
115 | typedef union {
|
---|
116 | RTC_REGISTER_C_BITS Bits;
|
---|
117 | UINT8 Data;
|
---|
118 | } RTC_REGISTER_C;
|
---|
119 |
|
---|
120 | //
|
---|
121 | // Register D
|
---|
122 | //
|
---|
123 | typedef struct {
|
---|
124 | UINT8 Reserved : 7; // Read as zero. Can not be written.
|
---|
125 | UINT8 Vrt : 1; // Valid RAM and Time
|
---|
126 | } RTC_REGISTER_D_BITS;
|
---|
127 |
|
---|
128 | typedef union {
|
---|
129 | RTC_REGISTER_D_BITS Bits;
|
---|
130 | UINT8 Data;
|
---|
131 | } RTC_REGISTER_D;
|
---|
132 |
|
---|
133 | #pragma pack()
|
---|
134 |
|
---|
135 | /**
|
---|
136 | Initialize RTC.
|
---|
137 |
|
---|
138 | @param Global For global use inside this module.
|
---|
139 |
|
---|
140 | @retval EFI_DEVICE_ERROR Initialization failed due to device error.
|
---|
141 | @retval EFI_SUCCESS Initialization successful.
|
---|
142 |
|
---|
143 | **/
|
---|
144 | EFI_STATUS
|
---|
145 | PcRtcInit (
|
---|
146 | IN PC_RTC_MODULE_GLOBALS *Global
|
---|
147 | );
|
---|
148 |
|
---|
149 | /**
|
---|
150 | Sets the current local time and date information.
|
---|
151 |
|
---|
152 | @param Time A pointer to the current time.
|
---|
153 | @param Global For global use inside this module.
|
---|
154 |
|
---|
155 | @retval EFI_SUCCESS The operation completed successfully.
|
---|
156 | @retval EFI_INVALID_PARAMETER A time field is out of range.
|
---|
157 | @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
|
---|
158 |
|
---|
159 | **/
|
---|
160 | EFI_STATUS
|
---|
161 | PcRtcSetTime (
|
---|
162 | IN EFI_TIME *Time,
|
---|
163 | IN PC_RTC_MODULE_GLOBALS *Global
|
---|
164 | );
|
---|
165 |
|
---|
166 | /**
|
---|
167 | Returns the current time and date information, and the time-keeping capabilities
|
---|
168 | of the hardware platform.
|
---|
169 |
|
---|
170 | @param Time A pointer to storage to receive a snapshot of the current time.
|
---|
171 | @param Capabilities An optional pointer to a buffer to receive the real time clock
|
---|
172 | device's capabilities.
|
---|
173 | @param Global For global use inside this module.
|
---|
174 |
|
---|
175 | @retval EFI_SUCCESS The operation completed successfully.
|
---|
176 | @retval EFI_INVALID_PARAMETER Time is NULL.
|
---|
177 | @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
|
---|
178 |
|
---|
179 | **/
|
---|
180 | EFI_STATUS
|
---|
181 | PcRtcGetTime (
|
---|
182 | OUT EFI_TIME *Time,
|
---|
183 | OUT EFI_TIME_CAPABILITIES *Capabilities OPTIONAL,
|
---|
184 | IN PC_RTC_MODULE_GLOBALS *Global
|
---|
185 | );
|
---|
186 |
|
---|
187 | /**
|
---|
188 | Sets the system wakeup alarm clock time.
|
---|
189 |
|
---|
190 | @param Enabled Enable or disable the wakeup alarm.
|
---|
191 | @param Time If Enable is TRUE, the time to set the wakeup alarm for.
|
---|
192 | If Enable is FALSE, then this parameter is optional, and may be NULL.
|
---|
193 | @param Global For global use inside this module.
|
---|
194 |
|
---|
195 | @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled.
|
---|
196 | If Enable is FALSE, then the wakeup alarm was disabled.
|
---|
197 | @retval EFI_INVALID_PARAMETER A time field is out of range.
|
---|
198 | @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
|
---|
199 | @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
|
---|
200 |
|
---|
201 | **/
|
---|
202 | EFI_STATUS
|
---|
203 | PcRtcSetWakeupTime (
|
---|
204 | IN BOOLEAN Enable,
|
---|
205 | IN EFI_TIME *Time OPTIONAL,
|
---|
206 | IN PC_RTC_MODULE_GLOBALS *Global
|
---|
207 | );
|
---|
208 |
|
---|
209 | /**
|
---|
210 | Returns the current wakeup alarm clock setting.
|
---|
211 |
|
---|
212 | @param Enabled Indicates if the alarm is currently enabled or disabled.
|
---|
213 | @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
|
---|
214 | @param Time The current alarm setting.
|
---|
215 | @param Global For global use inside this module.
|
---|
216 |
|
---|
217 | @retval EFI_SUCCESS The alarm settings were returned.
|
---|
218 | @retval EFI_INVALID_PARAMETER Enabled is NULL.
|
---|
219 | @retval EFI_INVALID_PARAMETER Pending is NULL.
|
---|
220 | @retval EFI_INVALID_PARAMETER Time is NULL.
|
---|
221 | @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
|
---|
222 | @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
|
---|
223 |
|
---|
224 | **/
|
---|
225 | EFI_STATUS
|
---|
226 | PcRtcGetWakeupTime (
|
---|
227 | OUT BOOLEAN *Enabled,
|
---|
228 | OUT BOOLEAN *Pending,
|
---|
229 | OUT EFI_TIME *Time,
|
---|
230 | IN PC_RTC_MODULE_GLOBALS *Global
|
---|
231 | );
|
---|
232 |
|
---|
233 | /**
|
---|
234 | The user Entry Point for PcRTC module.
|
---|
235 |
|
---|
236 | This is the entry point for PcRTC module. It installs the UEFI runtime service
|
---|
237 | including GetTime(),SetTime(),GetWakeupTime(),and SetWakeupTime().
|
---|
238 |
|
---|
239 | @param ImageHandle The firmware allocated handle for the EFI image.
|
---|
240 | @param SystemTable A pointer to the EFI System Table.
|
---|
241 |
|
---|
242 | @retval EFI_SUCCESS The entry point is executed successfully.
|
---|
243 | @retval Others Some error occurs when executing this entry point.
|
---|
244 |
|
---|
245 | **/
|
---|
246 | EFI_STATUS
|
---|
247 | EFIAPI
|
---|
248 | InitializePcRtc (
|
---|
249 | IN EFI_HANDLE ImageHandle,
|
---|
250 | IN EFI_SYSTEM_TABLE *SystemTable
|
---|
251 | );
|
---|
252 |
|
---|
253 | /**
|
---|
254 | See if all fields of a variable of EFI_TIME type is correct.
|
---|
255 |
|
---|
256 | @param Time The time to be checked.
|
---|
257 |
|
---|
258 | @retval EFI_INVALID_PARAMETER Some fields of Time are not correct.
|
---|
259 | @retval EFI_SUCCESS Time is a valid EFI_TIME variable.
|
---|
260 |
|
---|
261 | **/
|
---|
262 | EFI_STATUS
|
---|
263 | RtcTimeFieldsValid (
|
---|
264 | IN EFI_TIME *Time
|
---|
265 | );
|
---|
266 |
|
---|
267 | /**
|
---|
268 | Converts time from EFI_TIME format defined by UEFI spec to RTC format.
|
---|
269 |
|
---|
270 | This function converts time from EFI_TIME format defined by UEFI spec to RTC format.
|
---|
271 | If data mode of RTC is BCD, then converts EFI_TIME to it.
|
---|
272 | If RTC is in 12-hour format, then converts EFI_TIME to it.
|
---|
273 |
|
---|
274 | @param Time On input, the time data read from UEFI to convert
|
---|
275 | On output, the time converted to RTC format
|
---|
276 | @param RegisterB Value of Register B of RTC, indicating data mode
|
---|
277 | **/
|
---|
278 | VOID
|
---|
279 | ConvertEfiTimeToRtcTime (
|
---|
280 | IN OUT EFI_TIME *Time,
|
---|
281 | IN RTC_REGISTER_B RegisterB
|
---|
282 | );
|
---|
283 |
|
---|
284 | /**
|
---|
285 | Converts time read from RTC to EFI_TIME format defined by UEFI spec.
|
---|
286 |
|
---|
287 | This function converts raw time data read from RTC to the EFI_TIME format
|
---|
288 | defined by UEFI spec.
|
---|
289 | If data mode of RTC is BCD, then converts it to decimal,
|
---|
290 | If RTC is in 12-hour format, then converts it to 24-hour format.
|
---|
291 |
|
---|
292 | @param Time On input, the time data read from RTC to convert
|
---|
293 | On output, the time converted to UEFI format
|
---|
294 | @param RegisterB Value of Register B of RTC, indicating data mode
|
---|
295 | and hour format.
|
---|
296 |
|
---|
297 | @retval EFI_INVALID_PARAMETER Parameters passed in are invalid.
|
---|
298 | @retval EFI_SUCCESS Convert RTC time to EFI time successfully.
|
---|
299 |
|
---|
300 | **/
|
---|
301 | EFI_STATUS
|
---|
302 | ConvertRtcTimeToEfiTime (
|
---|
303 | IN OUT EFI_TIME *Time,
|
---|
304 | IN RTC_REGISTER_B RegisterB
|
---|
305 | );
|
---|
306 |
|
---|
307 | /**
|
---|
308 | Wait for a period for the RTC to be ready.
|
---|
309 |
|
---|
310 | @param Timeout Tell how long it should take to wait.
|
---|
311 |
|
---|
312 | @retval EFI_DEVICE_ERROR RTC device error.
|
---|
313 | @retval EFI_SUCCESS RTC is updated and ready.
|
---|
314 | **/
|
---|
315 | EFI_STATUS
|
---|
316 | RtcWaitToUpdate (
|
---|
317 | UINTN Timeout
|
---|
318 | );
|
---|
319 |
|
---|
320 | /**
|
---|
321 | See if field Day of an EFI_TIME is correct.
|
---|
322 |
|
---|
323 | @param Time Its Day field is to be checked.
|
---|
324 |
|
---|
325 | @retval TRUE Day field of Time is correct.
|
---|
326 | @retval FALSE Day field of Time is NOT correct.
|
---|
327 | **/
|
---|
328 | BOOLEAN
|
---|
329 | DayValid (
|
---|
330 | IN EFI_TIME *Time
|
---|
331 | );
|
---|
332 |
|
---|
333 | /**
|
---|
334 | Check if it is a leapyear.
|
---|
335 |
|
---|
336 | @param Time The time to be checked.
|
---|
337 |
|
---|
338 | @retval TRUE It is a leapyear.
|
---|
339 | @retval FALSE It is NOT a leapyear.
|
---|
340 | **/
|
---|
341 | BOOLEAN
|
---|
342 | IsLeapYear (
|
---|
343 | IN EFI_TIME *Time
|
---|
344 | );
|
---|
345 |
|
---|
346 | /**
|
---|
347 | Get the century RTC address from the ACPI FADT table.
|
---|
348 |
|
---|
349 | @return The century RTC address or 0 if not found.
|
---|
350 | **/
|
---|
351 | UINT8
|
---|
352 | GetCenturyRtcAddress (
|
---|
353 | VOID
|
---|
354 | );
|
---|
355 |
|
---|
356 | /**
|
---|
357 | Notification function of ACPI Table change.
|
---|
358 |
|
---|
359 | This is a notification function registered on ACPI Table change event.
|
---|
360 | It saves the Century address stored in ACPI FADT table.
|
---|
361 |
|
---|
362 | @param Event Event whose notification function is being invoked.
|
---|
363 | @param Context Pointer to the notification function's context.
|
---|
364 |
|
---|
365 | **/
|
---|
366 | VOID
|
---|
367 | EFIAPI
|
---|
368 | PcRtcAcpiTableChangeCallback (
|
---|
369 | IN EFI_EVENT Event,
|
---|
370 | IN VOID *Context
|
---|
371 | );
|
---|
372 |
|
---|
373 | #endif
|
---|