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