Browse Source

[time] Add RTC-based time source

Add a time source using the CMOS RTC to obtain the current time.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 years ago
parent
commit
12002d6955

+ 2
- 0
src/arch/i386/include/bits/time.h View File

@@ -9,4 +9,6 @@
9 9
 
10 10
 FILE_LICENCE ( GPL2_OR_LATER );
11 11
 
12
+#include <ipxe/rtc_time.h>
13
+
12 14
 #endif /* _BITS_TIME_H */

+ 18
- 0
src/arch/i386/include/ipxe/rtc_time.h View File

@@ -0,0 +1,18 @@
1
+#ifndef _IPXE_RTC_TIME_H
2
+#define _IPXE_RTC_TIME_H
3
+
4
+/** @file
5
+ *
6
+ * RTC-based time source
7
+ *
8
+ */
9
+
10
+FILE_LICENCE ( GPL2_OR_LATER );
11
+
12
+#ifdef TIME_RTC
13
+#define TIME_PREFIX_rtc
14
+#else
15
+#define TIME_PREFIX_rtc __rtc_
16
+#endif
17
+
18
+#endif /* _IPXE_RTC_TIME_H */

+ 83
- 0
src/arch/i386/include/rtc.h View File

@@ -0,0 +1,83 @@
1
+#ifndef _RTC_H
2
+#define _RTC_H
3
+
4
+/** @file
5
+ *
6
+ * CMOS Real-Time Clock (RTC)
7
+ *
8
+ * The CMOS/RTC registers are documented (with varying degrees of
9
+ * accuracy and consistency) at
10
+ *
11
+ *    http://www.nondot.org/sabre/os/files/MiscHW/RealtimeClockFAQ.txt
12
+ *    http://wiki.osdev.org/RTC
13
+ *    http://wiki.osdev.org/CMOS
14
+ */
15
+
16
+FILE_LICENCE ( GPL2_OR_LATER );
17
+
18
+#include <pic8259.h>
19
+
20
+/** RTC IRQ */
21
+#define RTC_IRQ 8
22
+
23
+/** RTC interrupt vector */
24
+#define RTC_INT IRQ_INT ( RTC_IRQ )
25
+
26
+/** CMOS/RTC address (and NMI) register */
27
+#define CMOS_ADDRESS 0x70
28
+
29
+/** NMI disable bit */
30
+#define CMOS_DISABLE_NMI 0x80
31
+
32
+/** CMOS/RTC data register */
33
+#define CMOS_DATA 0x71
34
+
35
+/** RTC seconds */
36
+#define RTC_SEC 0x00
37
+
38
+/** RTC minutes */
39
+#define RTC_MIN 0x02
40
+
41
+/** RTC hours */
42
+#define RTC_HOUR 0x04
43
+
44
+/** RTC weekday */
45
+#define RTC_WDAY 0x06
46
+
47
+/** RTC day of month */
48
+#define RTC_MDAY 0x07
49
+
50
+/** RTC month */
51
+#define RTC_MON 0x08
52
+
53
+/** RTC year */
54
+#define RTC_YEAR 0x09
55
+
56
+/** RTC status register A */
57
+#define RTC_STATUS_A 0x0a
58
+
59
+/** RTC update in progress bit */
60
+#define RTC_STATUS_A_UPDATE_IN_PROGRESS 0x80
61
+
62
+/** RTC status register B */
63
+#define RTC_STATUS_B 0x0b
64
+
65
+/** RTC 24 hour format bit */
66
+#define RTC_STATUS_B_24_HOUR 0x02
67
+
68
+/** RTC binary mode bit */
69
+#define RTC_STATUS_B_BINARY 0x04
70
+
71
+/** RTC Periodic Interrupt Enabled bit */
72
+#define RTC_STATUS_B_PIE 0x40
73
+
74
+/** RTC status register C */
75
+#define RTC_STATUS_C 0x0c
76
+
77
+/** RTC status register D */
78
+#define RTC_STATUS_D 0x0d
79
+
80
+/** CMOS default address */
81
+#define CMOS_DEFAULT_ADDRESS RTC_STATUS_D
82
+
83
+#endif /* _RTC_H */

+ 1
- 39
src/arch/i386/interface/pcbios/rtc_entropy.c View File

@@ -22,53 +22,15 @@ FILE_LICENCE ( GPL2_OR_LATER );
22 22
  *
23 23
  * RTC-based entropy source
24 24
  *
25
- * The CMOS/RTC registers are documented (with varying degrees of
26
- * accuracy and consistency) at
27
- *
28
- *    http://www.nondot.org/sabre/os/files/MiscHW/RealtimeClockFAQ.txt
29
- *    http://wiki.osdev.org/RTC
30
- *    http://wiki.osdev.org/CMOS
31 25
  */
32 26
 
33 27
 #include <stdint.h>
34 28
 #include <string.h>
35 29
 #include <biosint.h>
36 30
 #include <pic8259.h>
31
+#include <rtc.h>
37 32
 #include <ipxe/entropy.h>
38 33
 
39
-/** RTC IRQ */
40
-#define RTC_IRQ 8
41
-
42
-/** RTC interrupt vector */
43
-#define RTC_INT IRQ_INT ( RTC_IRQ )
44
-
45
-/** CMOS/RTC address (and NMI) register */
46
-#define CMOS_ADDRESS 0x70
47
-
48
-/** NMI disable bit */
49
-#define CMOS_DISABLE_NMI 0x80
50
-
51
-/** CMOS/RTC data register */
52
-#define CMOS_DATA 0x71
53
-
54
-/** RTC status register A */
55
-#define RTC_STATUS_A 0x0a
56
-
57
-/** RTC status register B */
58
-#define RTC_STATUS_B 0x0b
59
-
60
-/** RTC Periodic Interrupt Enabled bit */
61
-#define RTC_STATUS_B_PIE 0x40
62
-
63
-/** RTC status register C */
64
-#define RTC_STATUS_C 0x0c
65
-
66
-/** RTC status register D */
67
-#define RTC_STATUS_D 0x0d
68
-
69
-/** CMOS default address */
70
-#define CMOS_DEFAULT_ADDRESS RTC_STATUS_D
71
-
72 34
 /** RTC "interrupt triggered" flag */
73 35
 static uint8_t __text16 ( rtc_flag );
74 36
 #define rtc_flag __use_text16 ( rtc_flag )

+ 137
- 0
src/arch/i386/interface/pcbios/rtc_time.c View File

@@ -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 );

+ 1
- 1
src/config/defaults/pcbios.h View File

@@ -19,7 +19,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
19 19
 #define SMBIOS_PCBIOS
20 20
 #define SANBOOT_PCBIOS
21 21
 #define ENTROPY_RTC
22
-#define TIME_NULL
22
+#define TIME_RTC
23 23
 
24 24
 #define	IMAGE_ELF		/* ELF image support */
25 25
 #define	IMAGE_MULTIBOOT		/* MultiBoot image support */

Loading…
Cancel
Save