Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / i386 / interface / pcbios / rtc_time.c
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., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 /** @file
23  *
24  * RTC-based time source
25  *
26  */
27
28 #include <stdint.h>
29 #include <time.h>
30 #include <rtc.h>
31 #include <ipxe/time.h>
32
33 /**
34  * Read RTC register
35  *
36  * @v address           Register address
37  * @ret data            Data
38  */
39 static unsigned int rtc_readb ( int address ) {
40         outb ( address, CMOS_ADDRESS );
41         return inb ( CMOS_DATA );
42 }
43
44 /**
45  * Check if RTC update is in progress
46  *
47  * @ret is_busy         RTC update is in progress
48  */
49 static int rtc_is_busy ( void ) {
50         return ( rtc_readb ( RTC_STATUS_A ) & RTC_STATUS_A_UPDATE_IN_PROGRESS );
51 }
52
53 /**
54  * Read RTC BCD register
55  *
56  * @v address           Register address
57  * @ret value           Value
58  */
59 static unsigned int rtc_readb_bcd ( int address ) {
60         unsigned int bcd;
61
62         bcd = rtc_readb ( address );
63         return ( bcd - ( 6 * ( bcd >> 4 ) ) );
64 }
65
66 /**
67  * Read RTC time
68  *
69  * @ret time            Time, in seconds
70  */
71 static time_t rtc_read_time ( void ) {
72         unsigned int status_b;
73         int is_binary;
74         int is_24hour;
75         unsigned int ( * read_component ) ( int address );
76         struct tm tm;
77         int is_pm;
78         unsigned int hour;
79         time_t time;
80
81         /* Wait for any in-progress update to complete */
82         while ( rtc_is_busy() ) {}
83
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 );
89
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 );
94         if ( ! is_24hour ) {
95                 is_pm = ( hour >= 80 );
96                 hour = ( ( ( ( hour & 0x7f ) % 80 ) % 12 ) +
97                          ( is_pm ? 12 : 0 ) );
98         }
99         tm.tm_hour = hour;
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 */ );
105
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 ) );
110
111         /* Convert to seconds since the Epoch */
112         time = mktime ( &tm );
113
114         return time;
115 }
116
117 /**
118  * Get current time in seconds
119  *
120  * @ret time            Time, in seconds
121  */
122 static time_t rtc_now ( void ) {
123         time_t time = 0;
124         time_t last_time;
125
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
128          * an update.
129          */
130         do {
131                 last_time = time;
132                 time = rtc_read_time();
133         } while ( time != last_time );
134
135         return time;
136 }
137
138 PROVIDE_TIME ( rtc, time_now, rtc_now );