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
20 FILE_LICENCE ( GPL2_OR_LATER );
24 * RTC-based time source
31 #include <ipxe/time.h>
36 * @v address Register address
39 static unsigned int rtc_readb ( int address ) {
40 outb ( address, CMOS_ADDRESS );
41 return inb ( CMOS_DATA );
45 * Check if RTC update is in progress
47 * @ret is_busy RTC update is in progress
49 static int rtc_is_busy ( void ) {
50 return ( rtc_readb ( RTC_STATUS_A ) & RTC_STATUS_A_UPDATE_IN_PROGRESS );
54 * Read RTC BCD register
56 * @v address Register address
59 static unsigned int rtc_readb_bcd ( int address ) {
62 bcd = rtc_readb ( address );
63 return ( bcd - ( 6 * ( bcd >> 4 ) ) );
69 * @ret time Time, in seconds
71 static time_t rtc_read_time ( void ) {
72 unsigned int status_b;
75 unsigned int ( * read_component ) ( int address );
81 /* Wait for any in-progress update to complete */
82 while ( rtc_is_busy() ) {}
84 /* Determine RTC mode */
85 status_b = rtc_readb ( RTC_STATUS_B );
86 is_binary = ( status_b & RTC_STATUS_B_BINARY );
87 is_24hour = ( status_b & RTC_STATUS_B_24_HOUR );
88 read_component = ( is_binary ? rtc_readb : rtc_readb_bcd );
90 /* Read time values */
91 tm.tm_sec = read_component ( RTC_SEC );
92 tm.tm_min = read_component ( RTC_MIN );
93 hour = read_component ( RTC_HOUR );
95 is_pm = ( hour >= 80 );
96 hour = ( ( ( ( hour & 0x7f ) % 80 ) % 12 ) +
100 tm.tm_mday = read_component ( RTC_MDAY );
101 tm.tm_mon = ( read_component ( RTC_MON ) - 1 );
102 tm.tm_year = ( read_component ( RTC_YEAR ) +
103 100 /* Assume we are in the 21st century, since
104 * this code was written in 2012 */ );
106 DBGC ( RTC_STATUS_A, "RTCTIME is %04d-%02d-%02d %02d:%02d:%02d "
107 "(%s,%d-hour)\n", ( tm.tm_year + 1900 ), ( tm.tm_mon + 1 ),
108 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
109 ( is_binary ? "binary" : "BCD" ), ( is_24hour ? 24 : 12 ) );
111 /* Convert to seconds since the Epoch */
112 time = mktime ( &tm );
118 * Get current time in seconds
120 * @ret time Time, in seconds
122 static time_t rtc_now ( void ) {
126 /* Read time until we get two matching values in a row, in
127 * case we end up reading a corrupted value in the middle of
132 time = rtc_read_time();
133 } while ( time != last_time );
138 PROVIDE_TIME ( rtc, time_now, rtc_now );