summaryrefslogtreecommitdiffstats
path: root/qemu/roms/ipxe/src/arch/i386/interface/pcbios/rtc_time.c
blob: cdbeac8d598bb4aaf535685e04a056c8262cc6a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

/** @file
 *
 * RTC-based time source
 *
 */

#include <stdint.h>
#include <time.h>
#include <rtc.h>
#include <ipxe/time.h>

/**
 * Read RTC register
 *
 * @v address		Register address
 * @ret data		Data
 */
static unsigned int rtc_readb ( int address ) {
	outb ( address, CMOS_ADDRESS );
	return inb ( CMOS_DATA );
}

/**
 * Check if RTC update is in progress
 *
 * @ret is_busy		RTC update is in progress
 */
static int rtc_is_busy ( void ) {
	return ( rtc_readb ( RTC_STATUS_A ) & RTC_STATUS_A_UPDATE_IN_PROGRESS );
}

/**
 * Read RTC BCD register
 *
 * @v address		Register address
 * @ret value		Value
 */
static unsigned int rtc_readb_bcd ( int address ) {
	unsigned int bcd;

	bcd = rtc_readb ( address );
	return ( bcd - ( 6 * ( bcd >> 4 ) ) );
}

/**
 * Read RTC time
 *
 * @ret time		Time, in seconds
 */
static time_t rtc_read_time ( void ) {
	unsigned int status_b;
	int is_binary;
	int is_24hour;
	unsigned int ( * read_component ) ( int address );
	struct tm tm;
	int is_pm;
	unsigned int hour;
	time_t time;

	/* Wait for any in-progress update to complete */
	while ( rtc_is_busy() ) {}

	/* Determine RTC mode */
	status_b = rtc_readb ( RTC_STATUS_B );
	is_binary = ( status_b & RTC_STATUS_B_BINARY );
	is_24hour = ( status_b & RTC_STATUS_B_24_HOUR );
	read_component = ( is_binary ? rtc_readb : rtc_readb_bcd );

	/* Read time values */
	tm.tm_sec = read_component ( RTC_SEC );
	tm.tm_min = read_component ( RTC_MIN );
	hour = read_component ( RTC_HOUR );
	if ( ! is_24hour ) {
		is_pm = ( hour >= 80 );
		hour = ( ( ( ( hour & 0x7f ) % 80 ) % 12 ) +
			 ( is_pm ? 12 : 0 ) );
	}
	tm.tm_hour = hour;
	tm.tm_mday = read_component ( RTC_MDAY );
	tm.tm_mon = ( read_component ( RTC_MON ) - 1 );
	tm.tm_year = ( read_component ( RTC_YEAR ) +
		       100 /* Assume we are in the 21st century, since
			    * this code was written in 2012 */ );

	DBGC ( RTC_STATUS_A, "RTCTIME is %04d-%02d-%02d %02d:%02d:%02d "
	       "(%s,%d-hour)\n", ( tm.tm_year + 1900 ), ( tm.tm_mon + 1 ),
	       tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
	       ( is_binary ? "binary" : "BCD" ), ( is_24hour ? 24 : 12 ) );

	/* Convert to seconds since the Epoch */
	time = mktime ( &tm );

	return time;
}

/**
 * Get current time in seconds
 *
 * @ret time		Time, in seconds
 */
static time_t rtc_now ( void ) {
	time_t time = 0;
	time_t last_time;

	/* Read time until we get two matching values in a row, in
	 * case we end up reading a corrupted value in the middle of
	 * an update.
	 */
	do {
		last_time = time;
		time = rtc_read_time();
	} while ( time != last_time );

	return time;
}

PROVIDE_TIME ( rtc, time_now, rtc_now );