rtc: Fix Unix epoch conversion from RTC time.
[pintos-anon] / src / devices / rtc.c
1 #include "devices/rtc.h"
2 #include <round.h>
3 #include <stdio.h>
4 #include "threads/io.h"
5
6 /* This code is an interface to the MC146818A-compatible real
7    time clock found on PC motherboards.  See [MC146818A] for
8    hardware details. */
9
10 /* I/O register addresses. */
11 #define CMOS_REG_SET    0x70    /* Selects CMOS register exposed by REG_IO. */
12 #define CMOS_REG_IO     0x71    /* Contains the selected data byte. */
13
14 /* Indexes of CMOS registers with real-time clock functions.
15    Note that all of these registers are in BCD format,
16    so that 0x59 means 59, not 89. */
17 #define RTC_REG_SEC     0       /* Second: 0x00...0x59. */
18 #define RTC_REG_MIN     2       /* Minute: 0x00...0x59. */
19 #define RTC_REG_HOUR    4       /* Hour: 0x00...0x23. */
20 #define RTC_REG_MDAY    7       /* Day of the month: 0x01...0x31. */
21 #define RTC_REG_MON     8       /* Month: 0x01...0x12. */
22 #define RTC_REG_YEAR    9       /* Year: 0x00...0x99. */
23
24 /* Indexes of CMOS control registers. */
25 #define RTC_REG_A       0x0a    /* Register A: update-in-progress. */
26 #define RTC_REG_B       0x0b    /* Register B: 24/12 hour time, irq enables. */
27 #define RTC_REG_C       0x0c    /* Register C: pending interrupts. */
28 #define RTC_REG_D       0x0d    /* Register D: valid time? */
29
30 /* Register A. */
31 #define RTCSA_UIP       0x80    /* Set while time update in progress. */
32
33 /* Register B. */
34 #define RTCSB_SET       0x80    /* Disables update to let time be set. */
35 #define RTCSB_DM        0x04    /* 0 = BCD time format, 1 = binary format. */
36 #define RTCSB_24HR      0x02    /* 0 = 12-hour format, 1 = 24-hour format. */
37
38 static int bcd_to_bin (uint8_t);
39 static uint8_t cmos_read (uint8_t index);
40
41 /* Returns number of seconds since Unix epoch of January 1,
42    1970. */
43 time_t
44 rtc_get_time (void)
45 {
46   static const int days_per_month[12] =
47     {
48       31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
49     };
50   int sec, min, hour, mday, mon, year;
51   time_t time;
52   int i;
53
54   /* Get time components.
55
56      We repeatedly read the time until it is stable from one read
57      to another, in case we start our initial read in the middle
58      of an update.  This strategy is not recommended by the
59      MC146818A datasheet, but it is simpler than any of their
60      suggestions and, furthermore, it is also used by Linux.
61
62      The MC146818A can be configured for BCD or binary format,
63      but for historical reasons everyone always uses BCD format
64      except on obscure non-PC platforms, so we don't bother
65      trying to detect the format in use. */
66   do
67     {
68       sec = bcd_to_bin (cmos_read (RTC_REG_SEC));
69       min = bcd_to_bin (cmos_read (RTC_REG_MIN));
70       hour = bcd_to_bin (cmos_read (RTC_REG_HOUR));
71       mday = bcd_to_bin (cmos_read (RTC_REG_MDAY));
72       mon = bcd_to_bin (cmos_read (RTC_REG_MON));
73       year = bcd_to_bin (cmos_read (RTC_REG_YEAR));
74     }
75   while (sec != bcd_to_bin (cmos_read (RTC_REG_SEC)));
76
77   /* Translate years-since-1900 into years-since-1970.
78      If it's before the epoch, assume that it has passed 2000.
79      This will break at 2070, but that's long after our 31-bit
80      time_t breaks in 2038. */
81   if (year < 70)
82     year += 100;
83   year -= 70;
84
85   /* Break down all components into seconds. */
86   time = (year * 365 + DIV_ROUND_UP (year - 2, 4)) * 24 * 60 * 60;
87   for (i = 1; i < mon; i++)
88     time += days_per_month[i - 1] * 24 * 60 * 60;
89   if (mon > 2 && year % 4 == 2)
90     time += 24 * 60 * 60;
91   time += (mday - 1) * 24 * 60 * 60;
92   time += hour * 60 * 60;
93   time += min * 60;
94   time += sec;
95
96   return time;
97 }
98
99 /* Returns the integer value of the given BCD byte. */
100 static int
101 bcd_to_bin (uint8_t x)
102 {
103   return (x & 0x0f) + ((x >> 4) * 10);
104 }
105
106 /* Reads a byte from the CMOS register with the given INDEX and
107    returns the byte read. */
108 static uint8_t
109 cmos_read (uint8_t index)
110 {
111   outb (CMOS_REG_SET, index);
112   return inb (CMOS_REG_IO);
113 }