2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
28 * RTC-based time source
35 #include <ipxe/time.h>
40 * @v address Register address
43 static unsigned int rtc_readb ( int address ) {
44 outb ( address, CMOS_ADDRESS );
45 return inb ( CMOS_DATA );
49 * Check if RTC update is in progress
51 * @ret is_busy RTC update is in progress
53 static int rtc_is_busy ( void ) {
54 return ( rtc_readb ( RTC_STATUS_A ) & RTC_STATUS_A_UPDATE_IN_PROGRESS );
58 * Read RTC BCD register
60 * @v address Register address
63 static unsigned int rtc_readb_bcd ( int address ) {
66 bcd = rtc_readb ( address );
67 return ( bcd - ( 6 * ( bcd >> 4 ) ) );
73 * @ret time Time, in seconds
75 static time_t rtc_read_time ( void ) {
76 unsigned int status_b;
79 unsigned int ( * read_component ) ( int address );
85 /* Wait for any in-progress update to complete */
86 while ( rtc_is_busy() ) {}
88 /* Determine RTC mode */
89 status_b = rtc_readb ( RTC_STATUS_B );
90 is_binary = ( status_b & RTC_STATUS_B_BINARY );
91 is_24hour = ( status_b & RTC_STATUS_B_24_HOUR );
92 read_component = ( is_binary ? rtc_readb : rtc_readb_bcd );
94 /* Read time values */
95 tm.tm_sec = read_component ( RTC_SEC );
96 tm.tm_min = read_component ( RTC_MIN );
97 hour = read_component ( RTC_HOUR );
99 is_pm = ( hour >= 80 );
100 hour = ( ( ( ( hour & 0x7f ) % 80 ) % 12 ) +
101 ( is_pm ? 12 : 0 ) );
104 tm.tm_mday = read_component ( RTC_MDAY );
105 tm.tm_mon = ( read_component ( RTC_MON ) - 1 );
106 tm.tm_year = ( read_component ( RTC_YEAR ) +
107 100 /* Assume we are in the 21st century, since
108 * this code was written in 2012 */ );
110 DBGC ( RTC_STATUS_A, "RTCTIME is %04d-%02d-%02d %02d:%02d:%02d "
111 "(%s,%d-hour)\n", ( tm.tm_year + 1900 ), ( tm.tm_mon + 1 ),
112 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
113 ( is_binary ? "binary" : "BCD" ), ( is_24hour ? 24 : 12 ) );
115 /* Convert to seconds since the Epoch */
116 time = mktime ( &tm );
122 * Get current time in seconds
124 * @ret time Time, in seconds
126 static time_t rtc_now ( void ) {
130 /* Read time until we get two matching values in a row, in
131 * case we end up reading a corrupted value in the middle of
136 time = rtc_read_time();
137 } while ( time != last_time );
142 PROVIDE_TIME ( rtc, time_now, rtc_now );