Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/Assertions.h>
28#include <AK/LogStream.h>
29#include <Kernel/CMOS.h>
30#include <Kernel/RTC.h>
31
32namespace RTC {
33
34static time_t s_boot_time;
35
36void initialize()
37{
38 s_boot_time = now();
39}
40
41time_t boot_time()
42{
43 return s_boot_time;
44}
45
46static bool update_in_progress()
47{
48 return CMOS::read(0x0a) & 0x80;
49}
50
51inline bool is_leap_year(unsigned year)
52{
53 return ((year % 4 == 0) && ((year % 100 != 0) || (year % 400) == 0));
54}
55
56static unsigned days_in_months_since_start_of_year(unsigned month, unsigned year)
57{
58 ASSERT(month <= 11);
59 unsigned days = 0;
60 switch (month) {
61 case 11:
62 days += 30;
63 [[fallthrough]];
64 case 10:
65 days += 31;
66 [[fallthrough]];
67 case 9:
68 days += 30;
69 [[fallthrough]];
70 case 8:
71 days += 31;
72 [[fallthrough]];
73 case 7:
74 days += 31;
75 [[fallthrough]];
76 case 6:
77 days += 30;
78 [[fallthrough]];
79 case 5:
80 days += 31;
81 [[fallthrough]];
82 case 4:
83 days += 30;
84 [[fallthrough]];
85 case 3:
86 days += 31;
87 [[fallthrough]];
88 case 2:
89 if (is_leap_year(year))
90 days += 29;
91 else
92 days += 28;
93 [[fallthrough]];
94 case 1:
95 days += 31;
96 }
97 return days;
98}
99
100static unsigned days_in_years_since_epoch(unsigned year)
101{
102 unsigned days = 0;
103 while (year > 1969) {
104 days += 365;
105 if (is_leap_year(year))
106 ++days;
107 --year;
108 }
109 return days;
110}
111
112u8 bcd_to_binary(u8 bcd)
113{
114 return (bcd & 0x0F) + ((bcd >> 4) * 10);
115}
116
117void read_registers(unsigned& year, unsigned& month, unsigned& day, unsigned& hour, unsigned& minute, unsigned& second)
118{
119 while (update_in_progress())
120 ;
121
122 u8 status_b = CMOS::read(0x0b);
123
124 second = CMOS::read(0x00);
125 minute = CMOS::read(0x02);
126 hour = CMOS::read(0x04);
127 day = CMOS::read(0x07);
128 month = CMOS::read(0x08);
129 year = CMOS::read(0x09);
130
131 if (!(status_b & 0x04)) {
132 second = bcd_to_binary(second);
133 minute = bcd_to_binary(minute);
134 hour = bcd_to_binary(hour & 0x70);
135 day = bcd_to_binary(day);
136 month = bcd_to_binary(month);
137 year = bcd_to_binary(year);
138 }
139
140 if (!(status_b & 0x02) && (hour & 0x80)) {
141 hour = ((hour & 0x7F) + 12) % 24;
142 }
143
144 year += 2000;
145}
146
147time_t now()
148{
149 // FIXME: We should probably do something more robust here.
150 // Perhaps read all the values twice and verify that they were identical.
151 // We don't want to be caught in the middle of an RTC register update.
152 while (update_in_progress())
153 ;
154
155 unsigned year, month, day, hour, minute, second;
156 read_registers(year, month, day, hour, minute, second);
157
158 klog() << "RTC: Year: " << year << ", month: " << month << ", day: " << day;
159
160 ASSERT(year >= 2018);
161
162 return days_in_years_since_epoch(year - 1) * 86400
163 + days_in_months_since_start_of_year(month - 1, year) * 86400
164 + (day - 1) * 86400
165 + hour * 3600
166 + minute * 60
167 + second;
168}
169
170}