2 * Copyright (c) 1988 University of Utah.
3 * Copyright (c) 1982, 1990, 1993
4 * The Regents of the University of California.
5 * Copyright (c) 2011 The FreeBSD Foundation
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
12 * Portions of this software were developed by Julien Ridoux at the University
13 * of Melbourne under sponsorship from the FreeBSD Foundation.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * from: Utah $Hdr: clock.c 1.18 91/01/21$
40 * from: @(#)clock.c 8.2 (Berkeley) 1/12/94
41 * from: NetBSD: clock_subr.c,v 1.6 2001/07/07 17:04:02 thorpej Exp
43 * from: src/sys/i386/isa/clock.c,v 1.176 2001/09/04
47 * Helpers for time-of-day clocks. This is useful for architectures that need
48 * support multiple models of such clocks, and generally serves to make the
49 * code more machine-independent.
50 * If the clock in question can also be used as a time counter, the driver
51 * needs to initiate this.
52 * This code is not yet used by all architectures.
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
58 #include "opt_ffclock.h"
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/kernel.h>
64 #include <sys/clock.h>
65 #include <sys/sysctl.h>
67 #include <sys/timeffc.h>
69 #include <sys/timetc.h>
73 static device_t clock_dev = NULL;
74 static long clock_res;
75 static struct timespec clock_adj;
77 /* XXX: should be kern. now, it's no longer machdep. */
78 static int disable_rtc_set;
79 SYSCTL_INT(_machdep, OID_AUTO, disable_rtc_set, CTLFLAG_RW, &disable_rtc_set,
80 0, "Disallow adjusting time-of-day clock");
83 clock_register(device_t dev, long res) /* res has units of microseconds */
86 if (clock_dev != NULL) {
87 if (clock_res > res) {
89 device_printf(dev, "not installed as "
90 "time-of-day clock: clock %s has higher "
91 "resolution\n", device_get_name(clock_dev));
95 device_printf(clock_dev, "removed as "
96 "time-of-day clock: clock %s has higher "
97 "resolution\n", device_get_name(dev));
101 clock_adj.tv_sec = res / 2 / 1000000;
102 clock_adj.tv_nsec = res / 2 % 1000000 * 1000;
104 device_printf(dev, "registered as a time-of-day clock "
105 "(resolution %ldus, adjustment %jd.%09jds)\n", res,
106 (intmax_t)clock_adj.tv_sec, (intmax_t)clock_adj.tv_nsec);
110 * inittodr and settodr derived from the i386 versions written
111 * by Christoph Robitschko <chmr@edvz.tu-graz.ac.at>, reintroduced and
112 * updated by Chris Stenton <chris@gnome.co.uk> 8/10/94
116 * Initialize the time of day register, based on the time base which is, e.g.
120 inittodr(time_t base)
125 if (clock_dev == NULL) {
126 printf("warning: no time-of-day clock registered, system time "
127 "will not be set accurately\n");
130 /* XXX: We should poll all registered RTCs in case of failure */
131 error = CLOCK_GETTIME(clock_dev, &ts);
132 if (error != 0 && error != EINVAL) {
133 printf("warning: clock_gettime failed (%d), the system time "
134 "will not be set accurately\n", error);
137 if (error == EINVAL || ts.tv_sec < 0) {
138 printf("Invalid time in real time clock.\n"
139 "Check and reset the date immediately!\n");
143 ts.tv_sec += utc_offset();
144 timespecadd(&ts, &clock_adj);
147 ffclock_reset_clock(&ts);
160 * Write system time back to RTC
168 if (disable_rtc_set || clock_dev == NULL)
172 timespecadd(&ts, &clock_adj);
173 ts.tv_sec -= utc_offset();
174 /* XXX: We should really set all registered RTCs */
175 if ((error = CLOCK_SETTIME(clock_dev, &ts)) != 0)
176 printf("warning: clock_settime failed (%d), time-of-day clock "
177 "not adjusted to system time\n", error);