|
@@ -0,0 +1,137 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
|
|
3
|
+ *
|
|
4
|
+ * This program is free software; you can redistribute it and/or
|
|
5
|
+ * modify it under the terms of the GNU General Public License as
|
|
6
|
+ * published by the Free Software Foundation; either version 2 of the
|
|
7
|
+ * License, or any later version.
|
|
8
|
+ *
|
|
9
|
+ * This program is distributed in the hope that it will be useful, but
|
|
10
|
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
12
|
+ * General Public License for more details.
|
|
13
|
+ *
|
|
14
|
+ * You should have received a copy of the GNU General Public License
|
|
15
|
+ * along with this program; if not, write to the Free Software
|
|
16
|
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
17
|
+ */
|
|
18
|
+
|
|
19
|
+FILE_LICENCE ( GPL2_OR_LATER );
|
|
20
|
+
|
|
21
|
+/** @file
|
|
22
|
+ *
|
|
23
|
+ * RTC-based time source
|
|
24
|
+ *
|
|
25
|
+ */
|
|
26
|
+
|
|
27
|
+#include <stdint.h>
|
|
28
|
+#include <time.h>
|
|
29
|
+#include <rtc.h>
|
|
30
|
+#include <ipxe/time.h>
|
|
31
|
+
|
|
32
|
+/**
|
|
33
|
+ * Read RTC register
|
|
34
|
+ *
|
|
35
|
+ * @v address Register address
|
|
36
|
+ * @ret data Data
|
|
37
|
+ */
|
|
38
|
+static unsigned int rtc_readb ( int address ) {
|
|
39
|
+ outb ( address, CMOS_ADDRESS );
|
|
40
|
+ return inb ( CMOS_DATA );
|
|
41
|
+}
|
|
42
|
+
|
|
43
|
+/**
|
|
44
|
+ * Check if RTC update is in progress
|
|
45
|
+ *
|
|
46
|
+ * @ret is_busy RTC update is in progress
|
|
47
|
+ */
|
|
48
|
+static int rtc_is_busy ( void ) {
|
|
49
|
+ return ( rtc_readb ( RTC_STATUS_A ) & RTC_STATUS_A_UPDATE_IN_PROGRESS );
|
|
50
|
+}
|
|
51
|
+
|
|
52
|
+/**
|
|
53
|
+ * Read RTC BCD register
|
|
54
|
+ *
|
|
55
|
+ * @v address Register address
|
|
56
|
+ * @ret value Value
|
|
57
|
+ */
|
|
58
|
+static unsigned int rtc_readb_bcd ( int address ) {
|
|
59
|
+ unsigned int bcd;
|
|
60
|
+
|
|
61
|
+ bcd = rtc_readb ( address );
|
|
62
|
+ return ( bcd - ( 6 * ( bcd >> 4 ) ) );
|
|
63
|
+}
|
|
64
|
+
|
|
65
|
+/**
|
|
66
|
+ * Read RTC time
|
|
67
|
+ *
|
|
68
|
+ * @ret time Time, in seconds
|
|
69
|
+ */
|
|
70
|
+static time_t rtc_read_time ( void ) {
|
|
71
|
+ unsigned int status_b;
|
|
72
|
+ int is_binary;
|
|
73
|
+ int is_24hour;
|
|
74
|
+ unsigned int ( * read_component ) ( int address );
|
|
75
|
+ struct tm tm;
|
|
76
|
+ int is_pm;
|
|
77
|
+ unsigned int hour;
|
|
78
|
+ time_t time;
|
|
79
|
+
|
|
80
|
+ /* Wait for any in-progress update to complete */
|
|
81
|
+ while ( rtc_is_busy() ) {}
|
|
82
|
+
|
|
83
|
+ /* Determine RTC mode */
|
|
84
|
+ status_b = rtc_readb ( RTC_STATUS_B );
|
|
85
|
+ is_binary = ( status_b & RTC_STATUS_B_BINARY );
|
|
86
|
+ is_24hour = ( status_b & RTC_STATUS_B_24_HOUR );
|
|
87
|
+ read_component = ( is_binary ? rtc_readb : rtc_readb_bcd );
|
|
88
|
+
|
|
89
|
+ /* Read time values */
|
|
90
|
+ tm.tm_sec = read_component ( RTC_SEC );
|
|
91
|
+ tm.tm_min = read_component ( RTC_MIN );
|
|
92
|
+ hour = read_component ( RTC_HOUR );
|
|
93
|
+ if ( ! is_24hour ) {
|
|
94
|
+ is_pm = ( hour >= 80 );
|
|
95
|
+ hour = ( ( ( ( hour & 0x7f ) % 80 ) % 12 ) +
|
|
96
|
+ ( is_pm ? 12 : 0 ) );
|
|
97
|
+ }
|
|
98
|
+ tm.tm_hour = hour;
|
|
99
|
+ tm.tm_mday = read_component ( RTC_MDAY );
|
|
100
|
+ tm.tm_mon = ( read_component ( RTC_MON ) - 1 );
|
|
101
|
+ tm.tm_year = ( read_component ( RTC_YEAR ) +
|
|
102
|
+ 100 /* Assume we are in the 21st century, since
|
|
103
|
+ * this code was written in 2012 */ );
|
|
104
|
+
|
|
105
|
+ DBGC ( RTC_STATUS_A, "RTCTIME is %04d-%02d-%02d %02d:%02d:%02d "
|
|
106
|
+ "(%s,%d-hour)\n", ( tm.tm_year + 1900 ), ( tm.tm_mon + 1 ),
|
|
107
|
+ tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
|
|
108
|
+ ( is_binary ? "binary" : "BCD" ), ( is_24hour ? 24 : 12 ) );
|
|
109
|
+
|
|
110
|
+ /* Convert to seconds since the Epoch */
|
|
111
|
+ time = mktime ( &tm );
|
|
112
|
+
|
|
113
|
+ return time;
|
|
114
|
+}
|
|
115
|
+
|
|
116
|
+/**
|
|
117
|
+ * Get current time in seconds
|
|
118
|
+ *
|
|
119
|
+ * @ret time Time, in seconds
|
|
120
|
+ */
|
|
121
|
+static time_t rtc_now ( void ) {
|
|
122
|
+ time_t time = 0;
|
|
123
|
+ time_t last_time;
|
|
124
|
+
|
|
125
|
+ /* Read time until we get two matching values in a row, in
|
|
126
|
+ * case we end up reading a corrupted value in the middle of
|
|
127
|
+ * an update.
|
|
128
|
+ */
|
|
129
|
+ do {
|
|
130
|
+ last_time = time;
|
|
131
|
+ time = rtc_read_time();
|
|
132
|
+ } while ( time != last_time );
|
|
133
|
+
|
|
134
|
+ return time;
|
|
135
|
+}
|
|
136
|
+
|
|
137
|
+PROVIDE_TIME ( rtc, time_now, rtc_now );
|