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>
66 #include <sys/mutex.h>
67 #include <sys/sysctl.h>
69 #include <sys/timeffc.h>
71 #include <sys/timetc.h>
75 static device_t clock_dev = NULL;
76 static long clock_res;
77 static struct timespec clock_adj;
78 struct mtx resettodr_lock;
79 MTX_SYSINIT(resettodr_init, &resettodr_lock, "tod2rl", MTX_DEF);
81 /* XXX: should be kern. now, it's no longer machdep. */
82 static int disable_rtc_set;
83 SYSCTL_INT(_machdep, OID_AUTO, disable_rtc_set, CTLFLAG_RW, &disable_rtc_set,
84 0, "Disallow adjusting time-of-day clock");
87 clock_register(device_t dev, long res) /* res has units of microseconds */
90 if (clock_dev != NULL) {
91 if (clock_res <= res) {
93 device_printf(dev, "not installed as "
94 "time-of-day clock: clock %s has higher "
95 "resolution\n", device_get_name(clock_dev));
99 device_printf(clock_dev, "removed as "
100 "time-of-day clock: clock %s has higher "
101 "resolution\n", device_get_name(dev));
105 clock_adj.tv_sec = res / 2 / 1000000;
106 clock_adj.tv_nsec = res / 2 % 1000000 * 1000;
108 device_printf(dev, "registered as a time-of-day clock "
109 "(resolution %ldus, adjustment %jd.%09jds)\n", res,
110 (intmax_t)clock_adj.tv_sec, (intmax_t)clock_adj.tv_nsec);
114 * inittodr and settodr derived from the i386 versions written
115 * by Christoph Robitschko <chmr@edvz.tu-graz.ac.at>, reintroduced and
116 * updated by Chris Stenton <chris@gnome.co.uk> 8/10/94
120 * Initialize the time of day register, based on the time base which is, e.g.
124 inittodr(time_t base)
129 if (clock_dev == NULL) {
130 printf("warning: no time-of-day clock registered, system time "
131 "will not be set accurately\n");
134 /* XXX: We should poll all registered RTCs in case of failure */
135 mtx_lock(&resettodr_lock);
136 error = CLOCK_GETTIME(clock_dev, &ts);
137 mtx_unlock(&resettodr_lock);
138 if (error != 0 && error != EINVAL) {
139 printf("warning: clock_gettime failed (%d), the system time "
140 "will not be set accurately\n", error);
143 if (error == EINVAL || ts.tv_sec < 0) {
144 printf("Invalid time in real time clock.\n"
145 "Check and reset the date immediately!\n");
149 ts.tv_sec += utc_offset();
150 timespecadd(&ts, &clock_adj);
153 ffclock_reset_clock(&ts);
166 * Write system time back to RTC
174 if (disable_rtc_set || clock_dev == NULL)
178 timespecadd(&ts, &clock_adj);
179 ts.tv_sec -= utc_offset();
180 /* XXX: We should really set all registered RTCs */
181 mtx_lock(&resettodr_lock);
182 error = CLOCK_SETTIME(clock_dev, &ts);
183 mtx_unlock(&resettodr_lock);
185 printf("warning: clock_settime failed (%d), time-of-day clock "
186 "not adjusted to system time\n", error);