2 * Copyright (c) 1988 University of Utah.
3 * Copyright (c) 1982, 1990, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * from: Utah $Hdr: clock.c 1.18 91/01/21$
39 * from: @(#)clock.c 8.2 (Berkeley) 1/12/94
40 * from: NetBSD: clock_subr.c,v 1.6 2001/07/07 17:04:02 thorpej Exp
42 * from: src/sys/i386/isa/clock.c,v 1.176 2001/09/04
48 * Helpers for time-of-day clocks. This is useful for architectures that need
49 * support multiple models of such clocks, and generally serves to make the
50 * code more machine-independent.
51 * If the clock in question can also be used as a time counter, the driver
52 * needs to initiate this.
53 * This code is not yet used by all architectures.
57 * Generic routines to convert between a POSIX date
58 * (seconds since 1/1/1970) and yr/mo/day/hr/min/sec
59 * Derived from NetBSD arch/hp300/hp300/clock.c
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/kernel.h>
65 #include <sys/clock.h>
66 #include <sys/sysctl.h>
67 #include <sys/timetc.h>
69 /* XXX: for the CPU_* sysctl OID constants. */
70 #include <machine/cpu.h>
74 static __inline int leapyear(int year);
75 static int sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS);
78 #define days_in_year(y) (leapyear(y) ? 366 : 365)
79 #define days_in_month(y, m) \
80 (month_days[(m) - 1] + (m == FEBRUARY ? leapyear(y) : 0))
81 /* Day of week. Days are counted from 1/1/1970, which was a Thursday */
82 #define day_of_week(days) (((days) + 4) % 7)
84 static const int month_days[12] = {
85 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
88 static device_t clock_dev = NULL;
89 static long clock_res;
91 int adjkerntz; /* local offset from GMT in seconds */
92 int disable_rtc_set; /* disable resettodr() if != 0 */
93 int wall_cmos_clock; /* wall CMOS clock assumed if != 0 */
96 * These have traditionally been in machdep, but should probably be moved to
99 SYSCTL_PROC(_machdep, CPU_ADJKERNTZ, adjkerntz, CTLTYPE_INT|CTLFLAG_RW,
100 &adjkerntz, 0, sysctl_machdep_adjkerntz, "I", "");
102 SYSCTL_INT(_machdep, CPU_DISRTCSET, disable_rtc_set,
103 CTLFLAG_RW, &disable_rtc_set, 0, "");
105 SYSCTL_INT(_machdep, CPU_WALLCLOCK, wall_cmos_clock,
106 CTLFLAG_RW, &wall_cmos_clock, 0, "");
109 sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS)
112 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,
114 if (!error && req->newptr)
120 * This inline avoids some unnecessary modulo operations
121 * as compared with the usual macro:
122 * ( ((year % 4) == 0 &&
123 * (year % 100) != 0) ||
124 * ((year % 400) == 0) )
125 * It is otherwise equivalent.
132 if ((year & 3) == 0) {
134 if ((year % 100) == 0) {
136 if ((year % 400) == 0)
144 clock_ct_to_ts(struct clocktime *ct, struct timespec *ts)
152 if (ct->mon < 1 || ct->mon > 12 || ct->day < 1 ||
153 ct->day > days_in_month(year, ct->mon) ||
154 ct->hour > 23 || ct->min > 59 || ct->sec > 59 ||
155 ct->year > 2037) /* time_t overflow */
159 * Compute days since start of time
160 * First from years, then from months.
163 for (i = POSIX_BASE_YEAR; i < year; i++)
164 days += days_in_year(i);
167 for (i = 1; i < ct->mon; i++)
168 days += days_in_month(year, i);
169 days += (ct->day - 1);
171 /* Another sanity check. */
172 if (ct->dow != -1 && ct->dow != day_of_week(days))
175 /* Add hours, minutes, seconds. */
176 secs = ((days * 24 + ct->hour) * 60 + ct->min) * 60 + ct->sec;
179 ts->tv_nsec = ct->nsec;
184 clock_ts_to_ct(struct timespec *ts, struct clocktime *ct)
187 time_t rsec; /* remainder seconds */
191 days = secs / SECDAY;
192 rsec = secs % SECDAY;
194 ct->dow = day_of_week(days);
196 /* Subtract out whole years, counting them in i. */
197 for (year = POSIX_BASE_YEAR; days >= days_in_year(year); year++)
198 days -= days_in_year(year);
201 /* Subtract out whole months, counting them in i. */
202 for (i = 1; days >= days_in_month(year, i); i++)
203 days -= days_in_month(year, i);
206 /* Days are what is left over (+1) from all that. */
209 /* Hours, minutes, seconds are easy */
210 ct->hour = rsec / 3600;
215 ct->nsec = ts->tv_nsec;
219 clock_register(device_t dev, long res)
222 if (clock_dev != NULL) {
223 if (clock_res > res) {
225 device_printf(dev, "not installed as "
226 "time-of-day clock: clock %s has higher "
227 "resolution\n", device_get_name(clock_dev));
232 device_printf(clock_dev, "removed as "
233 "time-of-day clock: clock %s has higher "
234 "resolution\n", device_get_name(dev));
241 device_printf(dev, "registered as a time-of-day clock "
242 "(resolution %ldus)\n", res);
247 * inittodr and settodr derived from the i386 versions written
248 * by Christoph Robitschko <chmr@edvz.tu-graz.ac.at>, reintroduced and
249 * updated by Chris Stenton <chris@gnome.co.uk> 8/10/94
253 * Initialize the time of day register, based on the time base which is, e.g.
257 inittodr(time_t base)
259 struct timespec diff, ref, ts;
268 if (clock_dev == NULL) {
269 printf("warning: no time-of-day clock registered, system time "
270 "will not be set accurately\n");
273 error = CLOCK_GETTIME(clock_dev, &ts);
274 if (error != 0 && error != EINVAL) {
275 printf("warning: clock_gettime failed (%d), the system time "
276 "will not be set accurately\n", error);
279 if (error == EINVAL || ts.tv_sec < 0) {
280 printf("Invalid time in real time clock.\n");
281 printf("Check and reset the date immediately!\n");
284 ts.tv_sec += tz.tz_minuteswest * 60 +
285 (wall_cmos_clock ? adjkerntz : 0);
287 if (timespeccmp(&ref, &ts, >)) {
289 timespecsub(&ref, &ts);
292 timespecsub(&diff, &ref);
294 if (ts.tv_sec >= 2) {
295 /* badly off, adjust it */
301 * Write system time back to RTC
309 if (disable_rtc_set || clock_dev == NULL)
313 ts.tv_sec -= tz.tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0);
314 if ((error = CLOCK_SETTIME(clock_dev, &ts)) != 0) {
315 printf("warning: clock_settime failed (%d), time-of-day clock "
316 "not adjusted to system time\n", error);